Skip to content

Commit a661812

Browse files
committed
Add tests for core
1 parent 93fe7e2 commit a661812

2 files changed

Lines changed: 146 additions & 0 deletions

File tree

spec/core/model.test.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* eslint-disable no-unused-vars */
2+
import { describe, it, expect } from 'vitest';
3+
import Loader from '../../lib/loader.js';
4+
import settings from '../../config/settings.js';
5+
import Model from '../../app/core/model.js';
6+
7+
describe('Serializer', () => {
8+
class User extends Model {
9+
static {
10+
Model.prepare(this, ['id']);
11+
}
12+
13+
constructor(data) {
14+
super(data);
15+
this.id = data.id;
16+
this.name = data.name;
17+
this.email = data.email;
18+
}
19+
20+
static yUsers(records) {
21+
return records.where({ email: email => email.includes('y') });
22+
}
23+
24+
static does(records) {
25+
return records.where({ name: name => name.includes('Doe') });
26+
}
27+
}
28+
29+
const user1 = new User({ id: 'john-doe', name: 'John Doe', email: 'x@y.z' });
30+
const user2 = new User({ id: 'jane-doe', name: 'Jane Doe', email: 'a@b.c' });
31+
32+
describe('all', () => {
33+
it('returns all records', () => {
34+
expect(User.all).toEqual([user1, user2]);
35+
});
36+
});
37+
38+
describe('where', () => {
39+
it('returns exact matches correctly', () => {
40+
expect(User.where({ id: 'john-doe' })).toEqual([user1]);
41+
});
42+
43+
it('returns multiple matches correctly', () => {
44+
expect(User.where({ name: ['John Doe', 'Jane Doe'] })).toEqual([
45+
user1,
46+
user2,
47+
]);
48+
});
49+
50+
it('returns function matches correctly', () => {
51+
expect(User.where({ email: email => email.includes('y.z') })).toEqual([
52+
user1,
53+
]);
54+
});
55+
56+
it('returns multiple condition matches correctly', () => {
57+
expect(User.where({ id: 'john-doe', name: 'John Doe' })).toEqual([user1]);
58+
});
59+
});
60+
61+
describe('order', () => {
62+
it('returns records in the correct order', () => {
63+
expect(User.order((a, b) => a.name.localeCompare(b.name))).toEqual([
64+
user2,
65+
user1,
66+
]);
67+
});
68+
});
69+
70+
describe('scope', () => {
71+
it('returns the correct records with a single scope', () => {
72+
expect(User.scope('does')).toEqual([user1, user2]);
73+
});
74+
75+
it('returns the correct records with multiple scopes', () => {
76+
expect(User.scope('yUsers', 'does')).toEqual([user1]);
77+
});
78+
});
79+
80+
describe('find', () => {
81+
it('returns the correct record', () => {
82+
expect(User.find('john-doe')).toEqual(user1);
83+
});
84+
});
85+
86+
describe('search', () => {
87+
it('returns the correct record', () => {
88+
expect(User.search('john-doe')).toEqual(user1);
89+
expect(User.search('/john-doe')).toEqual(user1);
90+
expect(User.search('john-doe/')).toEqual(user1);
91+
expect(User.search('/JOHN-DoE/')).toEqual(user1);
92+
});
93+
});
94+
});

spec/core/serializer.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* eslint-disable no-unused-vars */
2+
import { describe, it, expect } from 'vitest';
3+
import Loader from '../../lib/loader.js';
4+
import settings from '../../config/settings.js';
5+
import Serializer from '../../app/core/serializer.js';
6+
7+
describe('Serializer', () => {
8+
class User {
9+
constructor(id, name, email) {
10+
this.id = id;
11+
this.name = name;
12+
this.email = email;
13+
}
14+
}
15+
16+
const user = new User(1, 'John Doe', 'x@y.z');
17+
18+
class UserSerializer extends Serializer {
19+
static {
20+
Serializer.prepare(this, [
21+
'id',
22+
['fullName', 'name'],
23+
['email', object => object.email.toUpperCase()],
24+
]);
25+
}
26+
}
27+
28+
describe('serialize', () => {
29+
it('returns serialized object', () => {
30+
const serialized = UserSerializer.serialize(user);
31+
expect(serialized).toEqual({
32+
id: 1,
33+
fullName: 'John Doe',
34+
email: 'X@Y.Z',
35+
});
36+
});
37+
});
38+
39+
describe('serializeArray', () => {
40+
it('returns serialized array', () => {
41+
const users = [user];
42+
const serialized = UserSerializer.serializeArray(users);
43+
expect(serialized).toEqual([
44+
{
45+
id: 1,
46+
fullName: 'John Doe',
47+
email: 'X@Y.Z',
48+
},
49+
]);
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)