|
1 | 1 | import { Entity, schema } from '@data-client/endpoint'; |
2 | 2 |
|
3 | 3 | import { initialState } from '../../state/reducer/createReducer'; |
| 4 | +import { State } from '../../types'; |
4 | 5 | import Controller from '../Controller'; |
5 | 6 |
|
6 | 7 | class Tacos extends Entity { |
@@ -46,37 +47,113 @@ describe('Controller.get()', () => { |
46 | 47 | () => controller.get(Tacos, { doesnotexist: 5 }, state); |
47 | 48 | }); |
48 | 49 |
|
49 | | - it('query Entity based on index', () => { |
| 50 | + describe('indexes', () => { |
50 | 51 | class User extends Entity { |
51 | 52 | id = ''; |
52 | 53 | username = ''; |
| 54 | + staff = false; |
53 | 55 |
|
54 | 56 | static indexes = ['username'] as const; |
55 | 57 | } |
| 58 | + it('query Entity based on index', () => { |
| 59 | + const controller = new Controller(); |
| 60 | + const state: State<unknown> = { |
| 61 | + ...initialState, |
| 62 | + entities: { |
| 63 | + User: { |
| 64 | + '1': { id: '1', username: 'bob' }, |
| 65 | + '2': { id: '2', username: 'george' }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + indexes: { |
| 69 | + User: { |
| 70 | + username: { |
| 71 | + bob: '1', |
| 72 | + george: '2', |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }; |
56 | 77 |
|
57 | | - const controller = new Controller(); |
58 | | - const state = { |
59 | | - ...initialState, |
60 | | - entities: { |
61 | | - User: { |
62 | | - '1': { id: '1', username: 'bob' }, |
| 78 | + const bob = controller.get(User, { username: 'bob' }, state); |
| 79 | + expect(bob).toBeDefined(); |
| 80 | + expect(bob).toBeInstanceOf(User); |
| 81 | + expect(bob).toMatchSnapshot(); |
| 82 | + // stability |
| 83 | + expect(controller.get(User, { username: 'bob' }, state)).toBe(bob); |
| 84 | + // should be same as id lookup |
| 85 | + expect(controller.get(User, { id: '1' }, state)).toBe(bob); |
| 86 | + // update index |
| 87 | + let nextState: State<unknown> = { |
| 88 | + ...state, |
| 89 | + entities: { |
| 90 | + ...state.entities, |
| 91 | + User: { |
| 92 | + ...state.entities.User, |
| 93 | + '1': { id: '1', username: 'george' }, |
| 94 | + '2': { id: '2', username: 'bob' }, |
| 95 | + }, |
63 | 96 | }, |
64 | | - }, |
65 | | - indexes: { |
66 | | - User: { |
67 | | - username: { |
68 | | - bob: '1', |
| 97 | + indexes: { |
| 98 | + ...state.indexes, |
| 99 | + User: { |
| 100 | + ...state.indexes.User, |
| 101 | + username: { |
| 102 | + ...state.indexes.User.username, |
| 103 | + bob: '2', |
| 104 | + george: '1', |
| 105 | + }, |
69 | 106 | }, |
70 | 107 | }, |
71 | | - }, |
72 | | - }; |
| 108 | + }; |
| 109 | + expect(controller.get(User, { username: 'bob' }, nextState)).not.toBe( |
| 110 | + bob, |
| 111 | + ); |
| 112 | + nextState = { |
| 113 | + ...state, |
| 114 | + entities: { |
| 115 | + ...state.entities, |
| 116 | + User: { |
| 117 | + ...state.entities.User, |
| 118 | + '1': { id: '1', username: 'bob', staff: true }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }; |
| 122 | + // update entity keep index |
| 123 | + const nextBob = controller.get(User, { username: 'bob' }, nextState); |
| 124 | + expect(nextBob).not.toBe(bob); |
| 125 | + expect(nextBob).toBeDefined(); |
| 126 | + expect(nextBob).toBeInstanceOf(User); |
| 127 | + expect(nextBob?.staff).toBe(true); |
| 128 | + }); |
73 | 129 |
|
74 | | - const bob = controller.get(User, { username: 'bob' }, state); |
75 | | - expect(bob).toBeDefined(); |
76 | | - expect(bob).toBeInstanceOf(User); |
77 | | - expect(bob).toMatchSnapshot(); |
78 | | - // should be same as id lookup |
79 | | - expect(bob).toBe(controller.get(User, { id: '1' }, state)); |
| 130 | + it('query indexes after empty state', () => { |
| 131 | + const controller = new Controller(); |
| 132 | + expect( |
| 133 | + controller.get(User, { username: 'bob' }, initialState), |
| 134 | + ).toBeUndefined(); |
| 135 | + const state: State<unknown> = { |
| 136 | + ...initialState, |
| 137 | + entities: { |
| 138 | + User: { |
| 139 | + '1': { id: '1', username: 'bob' }, |
| 140 | + '2': { id: '2', username: 'george' }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + indexes: { |
| 144 | + User: { |
| 145 | + username: { |
| 146 | + bob: '1', |
| 147 | + george: '2', |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }; |
| 152 | + const bob = controller.get(User, { username: 'bob' }, state); |
| 153 | + expect(bob).toBeDefined(); |
| 154 | + expect(bob).toBeInstanceOf(User); |
| 155 | + expect(bob).toMatchSnapshot(); |
| 156 | + }); |
80 | 157 | }); |
81 | 158 |
|
82 | 159 | it('query Collection based on args', () => { |
|
0 commit comments