Skip to content

Commit 4ef1a8f

Browse files
committed
Add tests for presenters
1 parent b680445 commit 4ef1a8f

8 files changed

+267
-313
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-disable no-unused-vars */
2+
import { describe, it, expect } from 'vitest';
3+
import Loader from '../../lib/loader.js';
4+
import BreadcrumbPresenter from '../../app/presenters/breadcrumbPresenter.js';
5+
import settings from '../../config/settings.js';
6+
7+
describe('BreadcrumbPresenter', () => {
8+
Loader.loadModules();
9+
const { CollectionFactory, SnippetFactory, CollectionSnippetFactory } =
10+
Loader.buildFactories();
11+
12+
const mainCollection = CollectionFactory.create('main');
13+
const primaryCollection = CollectionFactory.create('primary', {
14+
id: 'js',
15+
miniTitle: 'JavaScript',
16+
});
17+
const secondaryCollection = CollectionFactory.create('secondary', {
18+
id: 'js/c/array',
19+
parentId: 'js',
20+
miniTitle: 'Array',
21+
});
22+
const otherSecondaryCollection = CollectionFactory.create('secondary', {
23+
id: 'js/c/object',
24+
parentId: 'js',
25+
miniTitle: 'Object',
26+
});
27+
28+
const snippet = SnippetFactory.create({
29+
id: 'js/s/my-snippet',
30+
shortTitle: 'My snippet',
31+
tags: 'array;object',
32+
});
33+
34+
const collectionSnippetPrimary = CollectionSnippetFactory.create({
35+
collectionId: primaryCollection.id,
36+
snippetId: snippet.id,
37+
});
38+
const collectionSnippetSecondary = CollectionSnippetFactory.create({
39+
collectionId: secondaryCollection.id,
40+
snippetId: snippet.id,
41+
});
42+
const collectionSnippetOtherSecondary = CollectionSnippetFactory.create({
43+
collectionId: otherSecondaryCollection.id,
44+
snippetId: snippet.id,
45+
});
46+
47+
const snippetPresenter = new BreadcrumbPresenter(snippet);
48+
49+
describe('breadcrumbs', () => {
50+
it('returns the breadcrumbs for a snippet', () => {
51+
expect(snippetPresenter.breadcrumbs).toEqual([
52+
settings.breadcrumbs.home,
53+
{ url: '/js/p/1', name: 'JavaScript' },
54+
{ url: '/js/c/array/p/1', name: 'Array' },
55+
{ url: '/js/s/my-snippet', name: 'My snippet' },
56+
]);
57+
});
58+
});
59+
60+
describe('recommendedCollection', () => {
61+
it('returns the recommended collection for a snippet', () => {
62+
expect(snippetPresenter.recommendedCollection).toEqual(
63+
otherSecondaryCollection
64+
);
65+
});
66+
});
67+
});

spec/presenters/breadcrumb_presenter_spec.rb

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { describe, it, expect } from 'vitest';
2+
import Loader from '../../lib/loader.js';
3+
import CoverPresenter from '../../app/presenters/coverPresenter.js';
4+
5+
describe('CoverPresenter', () => {
6+
Loader.loadModules();
7+
const { CollectionFactory, SnippetFactory } = Loader.buildFactories();
8+
9+
const snippet = SnippetFactory.create({ cover: 'snippet-cover' });
10+
const collection = CollectionFactory.create({ cover: 'collection-cover' });
11+
12+
const snippetPresenter = new CoverPresenter(snippet);
13+
const collectionPresenter = new CoverPresenter(collection);
14+
15+
describe('coverUrl', () => {
16+
it('returns the cover url for a snippet', () => {
17+
expect(snippetPresenter.coverUrl()).toEqual(
18+
'/assets/cover/snippet-cover-400.webp'
19+
);
20+
});
21+
22+
it('returns the cover url for a snippet with full size', () => {
23+
expect(snippetPresenter.coverUrl(true)).toEqual(
24+
'/assets/cover/snippet-cover-800.webp'
25+
);
26+
});
27+
28+
it('returns the cover url for a collection', () => {
29+
expect(collectionPresenter.coverUrl()).toEqual(
30+
'/assets/splash/collection-cover-600.webp'
31+
);
32+
});
33+
});
34+
35+
describe('coverSrcset', () => {
36+
it('returns the cover srcset for a snippet', () => {
37+
expect(snippetPresenter.coverSrcset()).toEqual([
38+
'/assets/cover/snippet-cover-400.webp 400w',
39+
'/assets/cover/snippet-cover-800.webp 800w',
40+
]);
41+
});
42+
43+
it('returns the cover srcset for a snippet with full size', () => {
44+
expect(snippetPresenter.coverSrcset(true)).toEqual([
45+
'/assets/cover/snippet-cover-800.webp 800w',
46+
'/assets/cover/snippet-cover-400.webp 400w',
47+
'/assets/cover/snippet-cover-1200.webp 1200w',
48+
]);
49+
});
50+
51+
it('returns the cover srcset for a collection', () => {
52+
expect(collectionPresenter.coverSrcset()).toEqual([
53+
'/assets/splash/collection-cover-600.webp 600w',
54+
'/assets/splash/collection-cover-400.webp 400w',
55+
]);
56+
});
57+
});
58+
});

spec/presenters/cover_presenter_spec.rb

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* eslint-disable no-unused-vars */
2+
import { describe, it, expect } from 'vitest';
3+
import Loader from '../../lib/loader.js';
4+
import RecommendationPresenter from '../../app/presenters/recommendationPresenter.js';
5+
import settings from '../../config/settings.js';
6+
7+
describe('RecommendationPresenter', () => {
8+
Loader.loadModules();
9+
const { CollectionFactory, SnippetFactory, CollectionSnippetFactory } =
10+
Loader.buildFactories();
11+
12+
const snippet = SnippetFactory.create({
13+
languageId: 'javascript',
14+
id: 'js/s/my-snippet',
15+
shortTitle: 'My snippet',
16+
tags: 'array;object',
17+
});
18+
19+
const sameSnippetOtherLanguage = SnippetFactory.create({
20+
languageId: 'python',
21+
id: 'py/s/my-snippet',
22+
shortTitle: 'My snippet',
23+
tags: 'array;object',
24+
});
25+
26+
const unlistedSnippet = SnippetFactory.create('unlisted', {
27+
languageId: 'javascript',
28+
id: 'js/s/unrecommendable-snippet',
29+
shortTitle: 'Unrecommendable snippet',
30+
tags: 'array;object',
31+
});
32+
33+
const highlyRecommendableSnippet = SnippetFactory.create({
34+
languageId: 'javascript',
35+
id: 'js/s/highly-recommendable-snippet',
36+
shortTitle: 'Highly recommendable snippet',
37+
tags: 'array;object',
38+
});
39+
40+
const somewhatRecommendableSnippet = SnippetFactory.create({
41+
languageId: 'javascript',
42+
id: 'js/s/somewhat-recommendable-snippet',
43+
shortTitle: 'Somewhat recommendable snippet',
44+
tags: 'browser',
45+
});
46+
47+
const presenter = new RecommendationPresenter(snippet);
48+
49+
describe('#recommend_snippets', () => {
50+
const subject = presenter.recommendSnippets();
51+
52+
it('returns snippets that are not the current snippet', () => {
53+
expect(subject).not.toContainEqual(snippet);
54+
});
55+
56+
it('does not return the same snippet in another language', () => {
57+
expect(subject).not.toContainEqual(sameSnippetOtherLanguage);
58+
});
59+
60+
it('does not return unlisted snippets', () => {
61+
expect(subject).not.toContainEqual(unlistedSnippet);
62+
});
63+
64+
it('returns snippets in the same language with the same tags', () => {
65+
expect(subject).toContainEqual(highlyRecommendableSnippet);
66+
});
67+
68+
it('returns snippets in the same language with similar tags', () => {
69+
expect(subject).toContainEqual(somewhatRecommendableSnippet);
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)