|
| 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 | +}); |
0 commit comments