|
1 | | -import makeServer from './drivers/default/server'; |
2 | | -import Post from '../stubs/models/post'; |
3 | | -import { Batch } from '../../src/batch'; |
4 | | -import { Orion } from '../../src/orion'; |
5 | | - |
6 | | -let server: any; |
7 | | - |
8 | | -beforeEach(() => { |
9 | | - server = makeServer(); |
10 | | - Orion.setBaseUrl("https://api-mock.test/api") |
11 | | -}); |
12 | | - |
13 | | -afterEach(() => { |
14 | | - server.shutdown(); |
15 | | -}); |
16 | | - |
17 | | -describe('Batch tests', () => { |
18 | | - test('saving a couple of models', async () => { |
19 | | - const posts = [ |
20 | | - new Post(), |
21 | | - new Post(), |
22 | | - ] |
23 | | - posts[0].$attributes.title = "First"; |
24 | | - posts[1].$attributes.title = "Second"; |
25 | | - |
26 | | - const res = await Batch.store(posts); |
27 | | - |
28 | | - expect(server.schema.posts.all()).toHaveLength(2); |
29 | | - expect(server.schema.posts.find('1').attrs.title).toBe("First") |
30 | | - expect(server.schema.posts.find('2').attrs.title).toBe("Second") |
31 | | - expect(server.schema.posts.find('1').attrs.title).toEqual(res[0].$attributes.title) |
32 | | - expect(server.schema.posts.find('1').attrs.created_at).toEqual(res[0].$attributes.created_at) |
33 | | - expect(server.schema.posts.find('2').attrs.title).toEqual(res[1].$attributes.title) |
34 | | - expect(server.schema.posts.find('2').attrs.created_at).toEqual(res[1].$attributes.created_at) |
35 | | - }); |
36 | | - |
37 | | - test('updating a couple of models', async () => { |
38 | | - const posts = [ |
39 | | - new Post(), |
40 | | - new Post(), |
41 | | - new Post(), |
42 | | - ] |
43 | | - posts[0].$attributes.title = "First"; |
44 | | - posts[1].$attributes.title = "Second"; |
45 | | - posts[2].$attributes.title = "Third"; |
46 | | - |
47 | | - let res = await Batch.store(posts); |
48 | | - |
49 | | - res[0].$attributes.title = "NewFirst"; |
50 | | - res[1].$attributes.title = "NewSecond"; |
51 | | - |
52 | | - res = await Batch.update([res[0],res[1]]); |
53 | | - |
54 | | - expect(res).toHaveLength(2); |
55 | | - expect(server.schema.posts.find('1').attrs.title).toBe("NewFirst") |
56 | | - expect(server.schema.posts.find('2').attrs.title).toBe("NewSecond") |
57 | | - expect(server.schema.posts.find('1').attrs.title).toEqual(res[0].$attributes.title) |
58 | | - expect(server.schema.posts.find('2').attrs.title).toEqual(res[1].$attributes.title) |
59 | | - expect(server.schema.posts.find('3').attrs.title).toEqual("Third"); |
60 | | - |
61 | | - }); |
62 | | - |
63 | | - test('deleting a couple of models', async () => { |
64 | | - const posts = [ |
65 | | - new Post(), |
66 | | - new Post(), |
67 | | - new Post(), |
68 | | - ] |
69 | | - posts[0].$attributes.title = "First"; |
70 | | - posts[1].$attributes.title = "Second"; |
71 | | - posts[2].$attributes.title = "Third"; |
72 | | - |
73 | | - let res = await Batch.store(posts); |
74 | | - |
75 | | - let ModelDelete = await Batch.delete([res[1]]); |
76 | | - let idDelete = await Batch.delete([3], new Post); |
77 | | - |
78 | | - expect(server.schema.posts.find('1').attrs.deleted_at).toBeUndefined(); |
79 | | - expect(server.schema.posts.find('2').attrs.deleted_at).toBeDefined(); |
80 | | - expect(server.schema.posts.find('3').attrs.deleted_at).toBeDefined(); |
81 | | - expect(server.schema.posts.find('2').attrs.title).toEqual(ModelDelete[0].$attributes.title) |
82 | | - expect(server.schema.posts.find('3').attrs.title).toEqual(idDelete[0].$attributes.title) |
83 | | - |
84 | | - |
85 | | - }); |
86 | | - |
87 | | - test('restoring a couple of models', async () => { |
88 | | - const posts = [ |
89 | | - new Post(), |
90 | | - new Post(), |
91 | | - new Post(), |
92 | | - ] |
93 | | - posts[0].$attributes.title = "First"; |
94 | | - posts[1].$attributes.title = "Second"; |
95 | | - posts[2].$attributes.title = "Third"; |
96 | | - |
97 | | - let res = await Batch.store(posts); |
98 | | - |
99 | | - // delete ID 2 & 3 |
100 | | - let ModelDelete = await Batch.delete([res[1]]); |
101 | | - let idDelete = await Batch.delete([3], new Post); |
102 | | - |
103 | | - res = await Batch.restore([...ModelDelete, ...idDelete]); |
104 | | - |
105 | | - expect(server.schema.posts.find('1').attrs.deleted_at).toBeFalsy(); |
106 | | - expect(server.schema.posts.find('2').attrs.deleted_at).toBeFalsy(); |
107 | | - expect(server.schema.posts.find('3').attrs.deleted_at).toBeFalsy(); |
108 | | - expect(server.schema.posts.find('2').attrs.title).toEqual(res[0].$attributes.title); |
109 | | - expect(server.schema.posts.find('3').attrs.title).toEqual(res[1].$attributes.title); |
110 | | - |
111 | | - |
112 | | - }); |
113 | | -}); |
| 1 | +import makeServer from './drivers/default/server'; |
| 2 | +import Post from '../stubs/models/post'; |
| 3 | +import { Batch } from '../../src/batch'; |
| 4 | +import { Orion } from '../../src/orion'; |
| 5 | + |
| 6 | +let server: any; |
| 7 | + |
| 8 | +beforeEach(() => { |
| 9 | + server = makeServer(); |
| 10 | + Orion.setBaseUrl("https://api-mock.test/api") |
| 11 | +}); |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + server.shutdown(); |
| 15 | +}); |
| 16 | + |
| 17 | +describe('Batch tests', () => { |
| 18 | + test('saving a couple of models', async () => { |
| 19 | + const posts = [ |
| 20 | + new Post(), |
| 21 | + new Post(), |
| 22 | + ] |
| 23 | + posts[0].$attributes.title = "First"; |
| 24 | + posts[1].$attributes.title = "Second"; |
| 25 | + |
| 26 | + const res = await Batch.store(posts); |
| 27 | + |
| 28 | + expect(server.schema.posts.all()).toHaveLength(2); |
| 29 | + expect(server.schema.posts.find('1').attrs.title).toBe("First") |
| 30 | + expect(server.schema.posts.find('2').attrs.title).toBe("Second") |
| 31 | + expect(server.schema.posts.find('1').attrs.title).toEqual(res[0].$attributes.title) |
| 32 | + expect(server.schema.posts.find('1').attrs.created_at).toEqual(res[0].$attributes.created_at) |
| 33 | + expect(server.schema.posts.find('2').attrs.title).toEqual(res[1].$attributes.title) |
| 34 | + expect(server.schema.posts.find('2').attrs.created_at).toEqual(res[1].$attributes.created_at) |
| 35 | + }); |
| 36 | + |
| 37 | + test('updating a couple of models', async () => { |
| 38 | + const posts = [ |
| 39 | + new Post(), |
| 40 | + new Post(), |
| 41 | + new Post(), |
| 42 | + ] |
| 43 | + posts[0].$attributes.title = "First"; |
| 44 | + posts[1].$attributes.title = "Second"; |
| 45 | + posts[2].$attributes.title = "Third"; |
| 46 | + |
| 47 | + let res = await Batch.store(posts); |
| 48 | + |
| 49 | + res[0].$attributes.title = "NewFirst"; |
| 50 | + res[1].$attributes.title = "NewSecond"; |
| 51 | + |
| 52 | + res = await Batch.update([res[0],res[1]]); |
| 53 | + |
| 54 | + expect(res).toHaveLength(2); |
| 55 | + expect(server.schema.posts.find('1').attrs.title).toBe("NewFirst") |
| 56 | + expect(server.schema.posts.find('2').attrs.title).toBe("NewSecond") |
| 57 | + expect(server.schema.posts.find('1').attrs.title).toEqual(res[0].$attributes.title) |
| 58 | + expect(server.schema.posts.find('2').attrs.title).toEqual(res[1].$attributes.title) |
| 59 | + expect(server.schema.posts.find('3').attrs.title).toEqual("Third"); |
| 60 | + |
| 61 | + }); |
| 62 | + |
| 63 | + test('deleting a couple of models', async () => { |
| 64 | + const posts = [ |
| 65 | + new Post(), |
| 66 | + new Post(), |
| 67 | + new Post(), |
| 68 | + ] |
| 69 | + posts[0].$attributes.title = "First"; |
| 70 | + posts[1].$attributes.title = "Second"; |
| 71 | + posts[2].$attributes.title = "Third"; |
| 72 | + |
| 73 | + let res = await Batch.store(posts); |
| 74 | + |
| 75 | + let ModelDelete = await Batch.delete([res[1]]); |
| 76 | + let idDelete = await Batch.delete([3], new Post); |
| 77 | + |
| 78 | + expect(server.schema.posts.find('1').attrs.deleted_at).toBeUndefined(); |
| 79 | + expect(server.schema.posts.find('2').attrs.deleted_at).toBeDefined(); |
| 80 | + expect(server.schema.posts.find('3').attrs.deleted_at).toBeDefined(); |
| 81 | + expect(server.schema.posts.find('2').attrs.title).toEqual(ModelDelete[0].$attributes.title) |
| 82 | + expect(server.schema.posts.find('3').attrs.title).toEqual(idDelete[0].$attributes.title) |
| 83 | + |
| 84 | + |
| 85 | + }); |
| 86 | + |
| 87 | + test('restoring a couple of models', async () => { |
| 88 | + const posts = [ |
| 89 | + new Post(), |
| 90 | + new Post(), |
| 91 | + new Post(), |
| 92 | + ] |
| 93 | + posts[0].$attributes.title = "First"; |
| 94 | + posts[1].$attributes.title = "Second"; |
| 95 | + posts[2].$attributes.title = "Third"; |
| 96 | + |
| 97 | + let res = await Batch.store(posts); |
| 98 | + |
| 99 | + // delete ID 2 & 3 |
| 100 | + let ModelDelete = await Batch.delete([res[1]]); |
| 101 | + let idDelete = await Batch.delete([3], new Post); |
| 102 | + |
| 103 | + res = await Batch.restore([...ModelDelete, ...idDelete]); |
| 104 | + |
| 105 | + expect(server.schema.posts.find('1').attrs.deleted_at).toBeFalsy(); |
| 106 | + expect(server.schema.posts.find('2').attrs.deleted_at).toBeFalsy(); |
| 107 | + expect(server.schema.posts.find('3').attrs.deleted_at).toBeFalsy(); |
| 108 | + expect(server.schema.posts.find('2').attrs.title).toEqual(res[0].$attributes.title); |
| 109 | + expect(server.schema.posts.find('3').attrs.title).toEqual(res[1].$attributes.title); |
| 110 | + |
| 111 | + |
| 112 | + }); |
| 113 | +}); |
0 commit comments