|
| 1 | +import { createStore, assertState } from 'test/Helpers' |
| 2 | +import { Model, Attr, Str, BelongsTo, HasMany } from '@/index' |
| 3 | + |
| 4 | +describe('feature/relations/nested/nested_relations', () => { |
| 5 | + class User extends Model { |
| 6 | + static entity = 'users' |
| 7 | + |
| 8 | + @Attr() id!: number |
| 9 | + @Str('') name!: string |
| 10 | + |
| 11 | + @HasMany(() => Follower, 'userId') |
| 12 | + followers!: Follower[] |
| 13 | + } |
| 14 | + |
| 15 | + class Follower extends Model { |
| 16 | + static entity = 'followers' |
| 17 | + |
| 18 | + @Attr() id!: number |
| 19 | + @Attr() userId!: number |
| 20 | + } |
| 21 | + |
| 22 | + class Post extends Model { |
| 23 | + static entity = 'posts' |
| 24 | + |
| 25 | + @Attr() id!: number |
| 26 | + @Attr() userId!: number | null |
| 27 | + @Str('') title!: string |
| 28 | + |
| 29 | + @BelongsTo(() => User, 'userId') |
| 30 | + author!: User | null |
| 31 | + } |
| 32 | + |
| 33 | + it('inserts a nested relations with missing foreign key', async () => { |
| 34 | + const store = createStore() |
| 35 | + |
| 36 | + await store.$repo(Post).insert({ |
| 37 | + id: 1, |
| 38 | + userId: 1, |
| 39 | + title: 'Title 01', |
| 40 | + author: { |
| 41 | + id: 1, |
| 42 | + name: 'John Doe', |
| 43 | + followers: [{ id: 1 }, { id: 2 }] |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + assertState(store, { |
| 48 | + users: { |
| 49 | + 1: { id: 1, name: 'John Doe' } |
| 50 | + }, |
| 51 | + followers: { |
| 52 | + 1: { id: 1, userId: 1 }, |
| 53 | + 2: { id: 2, userId: 1 } |
| 54 | + }, |
| 55 | + posts: { |
| 56 | + 1: { id: 1, userId: 1, title: 'Title 01' } |
| 57 | + } |
| 58 | + }) |
| 59 | + }) |
| 60 | +}) |
0 commit comments