|
| 1 | +import { createStore } from 'test/Helpers' |
| 2 | +import { Model, Attr, HasOne, BelongsTo, HasMany, HasManyBy } from '@/index' |
| 3 | + |
| 4 | +describe('unit/model/Model_Relations', () => { |
| 5 | + class User extends Model { |
| 6 | + static entity = 'users' |
| 7 | + |
| 8 | + @Attr() id!: number |
| 9 | + @Attr() countryId!: number |
| 10 | + @Attr() nameIds!: number[] |
| 11 | + |
| 12 | + @HasOne(() => Phone, 'userId') |
| 13 | + phone!: Phone | null |
| 14 | + |
| 15 | + @BelongsTo(() => Country, 'countryId') |
| 16 | + country!: Country | null |
| 17 | + |
| 18 | + @HasMany(() => Post, 'userId') |
| 19 | + posts!: Post[] |
| 20 | + |
| 21 | + @HasManyBy(() => Name, 'nameIds') |
| 22 | + names!: Name[] |
| 23 | + } |
| 24 | + |
| 25 | + class Phone extends Model { |
| 26 | + static entity = 'phones' |
| 27 | + |
| 28 | + @Attr() id!: number |
| 29 | + @Attr() userId!: number |
| 30 | + } |
| 31 | + |
| 32 | + class Country extends Model { |
| 33 | + static entity = 'countries' |
| 34 | + |
| 35 | + @Attr() id!: number |
| 36 | + } |
| 37 | + |
| 38 | + class Post extends Model { |
| 39 | + static entity = 'posts' |
| 40 | + |
| 41 | + @Attr() id!: number |
| 42 | + @Attr() userId!: number |
| 43 | + } |
| 44 | + |
| 45 | + class Name extends Model { |
| 46 | + static entity = 'names' |
| 47 | + |
| 48 | + @Attr() id!: number |
| 49 | + } |
| 50 | + |
| 51 | + it('fills "has one" relation', () => { |
| 52 | + const store = createStore() |
| 53 | + |
| 54 | + const user = store.$repo(User).make({ |
| 55 | + id: 1, |
| 56 | + phone: { |
| 57 | + id: 2 |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + expect(user.phone).toBeInstanceOf(Phone) |
| 62 | + expect(user.phone!.id).toBe(2) |
| 63 | + }) |
| 64 | + |
| 65 | + it('fills "belongs to" relation', () => { |
| 66 | + const store = createStore() |
| 67 | + |
| 68 | + const user = store.$repo(User).make({ |
| 69 | + id: 1, |
| 70 | + country: { |
| 71 | + id: 2 |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + expect(user.country).toBeInstanceOf(Country) |
| 76 | + expect(user.country!.id).toBe(2) |
| 77 | + }) |
| 78 | + |
| 79 | + it('fills "has many" relation', () => { |
| 80 | + const store = createStore() |
| 81 | + |
| 82 | + const user = store.$repo(User).make({ |
| 83 | + id: 1, |
| 84 | + posts: [{ id: 2 }, { id: 3 }] |
| 85 | + }) |
| 86 | + |
| 87 | + expect(user.posts[0]).toBeInstanceOf(Post) |
| 88 | + expect(user.posts[1]).toBeInstanceOf(Post) |
| 89 | + expect(user.posts[0].id).toBe(2) |
| 90 | + expect(user.posts[1].id).toBe(3) |
| 91 | + }) |
| 92 | + |
| 93 | + it('fills "has many by" relation', () => { |
| 94 | + const store = createStore() |
| 95 | + |
| 96 | + const user = store.$repo(User).make({ |
| 97 | + id: 1, |
| 98 | + names: [{ id: 2 }, { id: 3 }] |
| 99 | + }) |
| 100 | + |
| 101 | + expect(user.names[0]).toBeInstanceOf(Name) |
| 102 | + expect(user.names[1]).toBeInstanceOf(Name) |
| 103 | + expect(user.names[0].id).toBe(2) |
| 104 | + expect(user.names[1].id).toBe(3) |
| 105 | + }) |
| 106 | +}) |
0 commit comments