Skip to content

Commit b680445

Browse files
committed
Add tests for models
1 parent cb4c4b0 commit b680445

File tree

7 files changed

+516
-563
lines changed

7 files changed

+516
-563
lines changed

spec/models/collection.test.js

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/* eslint-disable no-unused-vars */
2+
import { describe, it, expect } from 'vitest';
3+
import Collection from '../../app/models/collection.js';
4+
import Loader from '../../lib/loader.js';
5+
import settings from '../../config/settings.js';
6+
7+
describe('Collection', () => {
8+
Loader.loadModules();
9+
const { SnippetFactory, CollectionSnippetFactory, CollectionFactory } =
10+
Loader.buildFactories();
11+
12+
const mainCollection = CollectionFactory.create('main');
13+
const collectionsCollection = CollectionFactory.create('collections');
14+
15+
const primaryCollection = CollectionFactory.create('primary', {
16+
id: 'js',
17+
tokens: 'primary;collection;short;description',
18+
});
19+
20+
const secondaryCollection = CollectionFactory.create({
21+
id: 'js/c/array',
22+
parentId: primaryCollection.id,
23+
});
24+
25+
const otherSecondaryCollection = CollectionFactory.create({
26+
id: 'js/c/object',
27+
parentId: primaryCollection.id,
28+
});
29+
30+
const snippet = SnippetFactory.create();
31+
const collectionSnippet = CollectionSnippetFactory.create({
32+
snippetId: snippet.id,
33+
collectionId: primaryCollection.id,
34+
});
35+
36+
describe('.main', () => {
37+
it('returns main collection', () => {
38+
expect(Collection.main).toEqual(mainCollection);
39+
});
40+
});
41+
42+
describe('.collections', () => {
43+
it('returns collections collection', () => {
44+
expect(Collection.collections).toEqual(collectionsCollection);
45+
});
46+
});
47+
48+
describe('hasParent', () => {
49+
it('returns false if no parent', () => {
50+
expect(primaryCollection.hasParent).toEqual(false);
51+
});
52+
53+
it('returns true if parent exists', () => {
54+
expect(secondaryCollection.hasParent).toEqual(true);
55+
});
56+
});
57+
58+
describe('isMain', () => {
59+
it('returns true if main collection', () => {
60+
expect(mainCollection.isMain).toEqual(true);
61+
});
62+
63+
it('returns false if not main collection', () => {
64+
expect(primaryCollection.isMain).toEqual(false);
65+
});
66+
});
67+
68+
describe('isCollections', () => {
69+
it('returns true if collections collection', () => {
70+
expect(collectionsCollection.isCollections).toEqual(true);
71+
});
72+
73+
it('returns false if not collections collection', () => {
74+
expect(primaryCollection.isCollections).toEqual(false);
75+
});
76+
});
77+
78+
describe('isPrimary', () => {
79+
it('returns true if primary collection', () => {
80+
expect(primaryCollection.isPrimary).toEqual(true);
81+
});
82+
83+
it('returns false if not primary collection', () => {
84+
expect(secondaryCollection.isPrimary).toEqual(false);
85+
});
86+
});
87+
88+
describe('isSecondary', () => {
89+
it('returns false if primary collection', () => {
90+
expect(primaryCollection.isSecondary).toEqual(false);
91+
});
92+
93+
it('returns true if secondary collection', () => {
94+
expect(secondaryCollection.isSecondary).toEqual(true);
95+
});
96+
});
97+
98+
describe('rootUrl', () => {
99+
it('returns the slug if it is a parent', () => {
100+
expect(primaryCollection.rootUrl).toEqual(primaryCollection.slug);
101+
});
102+
103+
it('returns the parent slug if it has a parent', () => {
104+
expect(secondaryCollection.rootUrl).toEqual(
105+
secondaryCollection.parent.slug
106+
);
107+
});
108+
});
109+
110+
describe('siblings', () => {
111+
it('returns siblings if it has a parent', () => {
112+
expect(secondaryCollection.siblings).toEqual([
113+
secondaryCollection,
114+
otherSecondaryCollection,
115+
]);
116+
});
117+
118+
it('returns an empty array if it has no parent', () => {
119+
expect(primaryCollection.siblings).toEqual([]);
120+
});
121+
});
122+
123+
describe('siblingsExceptSelf', () => {
124+
it('returns siblings except self if it has a parent', () => {
125+
expect(secondaryCollection.siblingsExceptSelf).toEqual([
126+
otherSecondaryCollection,
127+
]);
128+
});
129+
130+
it('returns an empty array if it has no parent', () => {
131+
expect(primaryCollection.siblingsExceptSelf).toEqual([]);
132+
});
133+
});
134+
135+
describe('searchTokensArray', () => {
136+
it('returns tokens', () => {
137+
expect(primaryCollection.searchTokensArray).toEqual([
138+
'primary',
139+
'collection',
140+
'short',
141+
'description',
142+
]);
143+
});
144+
});
145+
146+
describe('firstPageSlug', () => {
147+
it('returns first page slug', () => {
148+
expect(primaryCollection.firstPageSlug).toEqual(
149+
`${primaryCollection.slug}/p/1`
150+
);
151+
});
152+
});
153+
154+
describe('listedSnippets', () => {
155+
it('returns listed snippets', () => {
156+
expect(primaryCollection.listedSnippets).toEqual([snippet]);
157+
});
158+
});
159+
160+
describe('formattedSnippetCount', () => {
161+
it('returns formatted snippet count', () => {
162+
expect(primaryCollection.formattedSnippetCount).toEqual('1 snippets');
163+
});
164+
});
165+
166+
describe('pageCount', () => {
167+
it('returns page count', () => {
168+
expect(primaryCollection.pageCount).toEqual(1);
169+
});
170+
});
171+
172+
describe('allPageSlugs', () => {
173+
it('returns all page slugs', () => {
174+
expect(primaryCollection.allPageSlugs).toEqual([
175+
`${primaryCollection.slug}/p/1`,
176+
]);
177+
});
178+
});
179+
180+
describe('allPageFullUrls', () => {
181+
it('returns all page full urls', () => {
182+
expect(primaryCollection.allPageFullUrls).toEqual([
183+
`${settings.website.url}${primaryCollection.slug}/p/1`,
184+
]);
185+
});
186+
});
187+
188+
describe('matchesTag', () => {
189+
it('returns true if collection matches tag', () => {
190+
expect(secondaryCollection.matchesTag('array')).toEqual(true);
191+
});
192+
193+
it('returns false if collection does not match tag', () => {
194+
expect(secondaryCollection.matchesTag('js')).toEqual(false);
195+
});
196+
});
197+
198+
describe('pages', () => {
199+
it('returns page objects with the correct snippet', () => {
200+
expect(primaryCollection.pages[0].key).toEqual('js/p/1');
201+
});
202+
});
203+
});

0 commit comments

Comments
 (0)