|
| 1 | +import { |
| 2 | + getRelatedResources, getResource, getResourceData, getResources, getResourceTree |
| 3 | +} from '../src/jsonapi'; |
| 4 | + |
| 5 | +const state = { |
| 6 | + api: { |
| 7 | + endpoint: { |
| 8 | + axiosConfig: {} |
| 9 | + }, |
| 10 | + users: { |
| 11 | + data: [ |
| 12 | + { |
| 13 | + type: 'users', |
| 14 | + id: '1', |
| 15 | + attributes: { |
| 16 | + name: 'John Doe' |
| 17 | + }, |
| 18 | + relationships: { |
| 19 | + transactions: { |
| 20 | + data: [ |
| 21 | + { |
| 22 | + type: 'transactions', |
| 23 | + id: '34' |
| 24 | + } |
| 25 | + ] |
| 26 | + } |
| 27 | + } |
| 28 | + }, |
| 29 | + { |
| 30 | + type: 'users', |
| 31 | + id: '2', |
| 32 | + attributes: { |
| 33 | + name: 'Emily Jane' |
| 34 | + }, |
| 35 | + relationships: { |
| 36 | + companies: { |
| 37 | + data: null |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + ] |
| 42 | + }, |
| 43 | + transactions: { |
| 44 | + data: [ |
| 45 | + { |
| 46 | + type: 'transactions', |
| 47 | + id: '34', |
| 48 | + attributes: { |
| 49 | + description: 'ABC', |
| 50 | + createdAt: '2016-02-12T13:34:01+0000', |
| 51 | + updatedAt: '2016-02-19T11:52:43+0000', |
| 52 | + }, |
| 53 | + relationships: { |
| 54 | + task: { |
| 55 | + data: null |
| 56 | + } |
| 57 | + }, |
| 58 | + links: { |
| 59 | + self: 'http://localhost/transactions/34' |
| 60 | + } |
| 61 | + } |
| 62 | + ] |
| 63 | + }, |
| 64 | + isCreating: 0, |
| 65 | + isReading: 0, |
| 66 | + isUpdating: 0, |
| 67 | + isDeleting: 0 |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +describe('Resource tree selector', () => { |
| 72 | + it('should return the full resource tree', () => { |
| 73 | + const resourceTree = getResourceTree(state, 'transactions'); |
| 74 | + |
| 75 | + expect(resourceTree).toEqual(state.api.transactions); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should not break if resource does not exist', () => { |
| 79 | + const resourceTree = getResourceTree(state, 'unicorns'); |
| 80 | + |
| 81 | + expect(resourceTree) |
| 82 | + .toEqual({ data: [] }); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('Resource data selector', () => { |
| 87 | + it('should return the full resource data', () => { |
| 88 | + const resourceTree = getResourceData(state, 'transactions'); |
| 89 | + |
| 90 | + expect(resourceTree) |
| 91 | + .toEqual(state.api.transactions.data); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should not break if resource tree does not have a data attribute', () => { |
| 95 | + state.types = {}; |
| 96 | + const resourceTree = getResourceData(state, 'types'); |
| 97 | + |
| 98 | + expect(resourceTree) |
| 99 | + .toEqual([]); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should not break if resource does not exist', () => { |
| 103 | + const resourceTree = getResourceData(state, 'unicorns'); |
| 104 | + |
| 105 | + expect(resourceTree) |
| 106 | + .toEqual([]); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('Resource selector', () => { |
| 111 | + it('should return the correct resource', () => { |
| 112 | + const resourceTree = getResource(state, { type: 'users', id: '1' }); |
| 113 | + |
| 114 | + expect(resourceTree) |
| 115 | + .toEqual(state.api.users.data[0]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should support alternative inputs and the correct resource', () => { |
| 119 | + const resourceTree = getResource(state, 'users', '1'); |
| 120 | + |
| 121 | + expect(resourceTree) |
| 122 | + .toEqual(state.api.users.data[0]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should support passing an integer as an id', () => { |
| 126 | + const resourceTree = getResource(state, 'users', 1); |
| 127 | + |
| 128 | + expect(resourceTree) |
| 129 | + .toEqual(state.api.users.data[0]); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should not break if resource does not have exist', () => { |
| 133 | + state.types = {}; |
| 134 | + const resourceTree = getResource(state, { |
| 135 | + type: 'users', |
| 136 | + id: '10' |
| 137 | + }); |
| 138 | + |
| 139 | + expect(resourceTree) |
| 140 | + .toEqual(null); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should not break if resource type does not have exist', () => { |
| 144 | + state.types = {}; |
| 145 | + const resourceTree = getResource(state, { |
| 146 | + type: 'unicorns', |
| 147 | + id: '10' |
| 148 | + }); |
| 149 | + |
| 150 | + expect(resourceTree) |
| 151 | + .toEqual(null); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('Resources selector', () => { |
| 156 | + it('should return the correct resources', () => { |
| 157 | + const resourceTree = getResources(state, 'users', ['1', '2']); |
| 158 | + |
| 159 | + expect(resourceTree) |
| 160 | + .toEqual(state.api.users.data); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should return the correct resources with an array of identifiers', () => { |
| 164 | + const resourceTree = getResources(state, [ |
| 165 | + { |
| 166 | + type: 'users', |
| 167 | + id: '1', |
| 168 | + }, |
| 169 | + { |
| 170 | + type: 'users', |
| 171 | + id: '2', |
| 172 | + } |
| 173 | + ]); |
| 174 | + |
| 175 | + expect(resourceTree) |
| 176 | + .toEqual(state.api.users.data); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should support passing an integer as an id', () => { |
| 180 | + const resourceTree = getResources(state, 'users', [1, 2]); |
| 181 | + |
| 182 | + expect(resourceTree) |
| 183 | + .toEqual(state.api.users.data); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should not break if a resource does not have exist', () => { |
| 187 | + const resourceTree = getResources(state, 'users', ['1', '2', '3']); |
| 188 | + |
| 189 | + expect(resourceTree) |
| 190 | + .toEqual(state.api.users.data); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should not break if resource type does not have exist', () => { |
| 194 | + state.types = {}; |
| 195 | + const resourceTree = getResources(state, 'unicorns', ['1']); |
| 196 | + |
| 197 | + expect(resourceTree) |
| 198 | + .toEqual([]); |
| 199 | + }); |
| 200 | +}); |
| 201 | + |
| 202 | +describe('Relationship selector', () => { |
| 203 | + it('should return the correct relationships', () => { |
| 204 | + const relationshipResources = getRelatedResources(state, state.api.users.data[0], 'transactions'); |
| 205 | + |
| 206 | + expect(relationshipResources) |
| 207 | + .toEqual([state.api.transactions.data[0]]); |
| 208 | + }); |
| 209 | +}); |
0 commit comments