|
| 1 | +import attributeGroupSyncFn from '../src/attribute-groups' |
| 2 | +import { baseActionsList } from '../src/attribute-groups-actions' |
| 3 | + |
| 4 | +describe('Exports', () => { |
| 5 | + test('correctly define base actions list', () => { |
| 6 | + expect(baseActionsList).toEqual([ |
| 7 | + { action: 'changeName', key: 'name' }, |
| 8 | + { action: 'setKey', key: 'key' }, |
| 9 | + { action: 'setDescription', key: 'description' }, |
| 10 | + ]) |
| 11 | + }) |
| 12 | +}) |
| 13 | + |
| 14 | +describe('Actions', () => { |
| 15 | + let attributeGroupSync |
| 16 | + beforeEach(() => { |
| 17 | + attributeGroupSync = attributeGroupSyncFn() |
| 18 | + }) |
| 19 | + |
| 20 | + test('should build `changeName` action', () => { |
| 21 | + const before = { |
| 22 | + name: 'John', |
| 23 | + } |
| 24 | + const now = { |
| 25 | + name: 'Robert', |
| 26 | + } |
| 27 | + |
| 28 | + const actual = attributeGroupSync.buildActions(now, before) |
| 29 | + const expected = [{ action: 'changeName', name: now.name }] |
| 30 | + expect(actual).toEqual(expected) |
| 31 | + }) |
| 32 | + |
| 33 | + test('should build `setDescription` action', () => { |
| 34 | + const before = { |
| 35 | + description: 'some description', |
| 36 | + } |
| 37 | + const now = { |
| 38 | + description: 'some updated description', |
| 39 | + } |
| 40 | + |
| 41 | + const actual = attributeGroupSync.buildActions(now, before) |
| 42 | + const expected = [ |
| 43 | + { |
| 44 | + action: 'setDescription', |
| 45 | + description: now.description, |
| 46 | + }, |
| 47 | + ] |
| 48 | + expect(actual).toEqual(expected) |
| 49 | + }) |
| 50 | + |
| 51 | + test('should build `setKey` action', () => { |
| 52 | + const before = { |
| 53 | + key: 'some-key', |
| 54 | + } |
| 55 | + const now = { |
| 56 | + key: 'new-key', |
| 57 | + } |
| 58 | + |
| 59 | + const actual = attributeGroupSync.buildActions(now, before) |
| 60 | + const expected = [ |
| 61 | + { |
| 62 | + action: 'setKey', |
| 63 | + key: now.key, |
| 64 | + }, |
| 65 | + ] |
| 66 | + expect(actual).toEqual(expected) |
| 67 | + }) |
| 68 | + |
| 69 | + describe('`addAttribute`', () => { |
| 70 | + test('should build `addAttribute` action with one attribute', () => { |
| 71 | + const before = { |
| 72 | + attributes: [], |
| 73 | + } |
| 74 | + const now = { attributes: [{ key: 'Size' }] } |
| 75 | + |
| 76 | + const actual = attributeGroupSync.buildActions(now, before) |
| 77 | + const expected = [ |
| 78 | + { action: 'addAttribute', attribute: now.attributes[0] }, |
| 79 | + ] |
| 80 | + expect(actual).toEqual(expected) |
| 81 | + }) |
| 82 | + test('should build `addAttribute` action with two attributes', () => { |
| 83 | + const before = { attributes: [] } |
| 84 | + const now = { attributes: [{ key: 'Size' }, { key: 'Brand' }] } |
| 85 | + |
| 86 | + const actual = attributeGroupSync.buildActions(now, before) |
| 87 | + const expected = [ |
| 88 | + { action: 'addAttribute', attribute: now.attributes[0] }, |
| 89 | + { action: 'addAttribute', attribute: now.attributes[1] }, |
| 90 | + ] |
| 91 | + expect(actual).toEqual(expected) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe('`removeAttribute`', () => { |
| 96 | + test('should build `removeAttribute` action removing one attribute', () => { |
| 97 | + const before = { |
| 98 | + attributes: [{ key: 'Size' }, { key: 'Brand' }], |
| 99 | + } |
| 100 | + const now = { attributes: [{ key: 'Size' }] } |
| 101 | + |
| 102 | + const actual = attributeGroupSync.buildActions(now, before) |
| 103 | + const expected = [ |
| 104 | + { action: 'removeAttribute', attribute: before.attributes[1] }, |
| 105 | + ] |
| 106 | + expect(actual).toEqual(expected) |
| 107 | + }) |
| 108 | + test('should build `removeAttribute` action removing two attributes', () => { |
| 109 | + const before = { |
| 110 | + attributes: [{ key: 'Size' }, { key: 'Brand' }], |
| 111 | + } |
| 112 | + const now = { attributes: [] } |
| 113 | + |
| 114 | + const actual = attributeGroupSync.buildActions(now, before) |
| 115 | + const expected = [ |
| 116 | + { action: 'removeAttribute', attribute: before.attributes[0] }, |
| 117 | + { action: 'removeAttribute', attribute: before.attributes[1] }, |
| 118 | + ] |
| 119 | + expect(actual).toEqual(expected) |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + describe('Swap attributes (create one + delete one)', () => { |
| 124 | + test('should build `removeAttribute` and `addAttribute`', () => { |
| 125 | + const before = { attributes: [{ key: 'Size' }] } |
| 126 | + const now = { attributes: [{ key: 'Brand' }] } |
| 127 | + |
| 128 | + const actual = attributeGroupSync.buildActions(now, before) |
| 129 | + const expected = [ |
| 130 | + { action: 'removeAttribute', attribute: before.attributes[0] }, |
| 131 | + { action: 'addAttribute', attribute: now.attributes[0] }, |
| 132 | + ] |
| 133 | + expect(actual).toEqual(expected) |
| 134 | + }) |
| 135 | + }) |
| 136 | + |
| 137 | + describe('Multiple actions', () => { |
| 138 | + test('should build multiple actions for required changes', () => { |
| 139 | + const before = { |
| 140 | + attributes: [{ key: 'Size' }, { key: 'Brand' }], |
| 141 | + } |
| 142 | + const now = { |
| 143 | + attributes: [{ key: 'Quality' }, { key: 'Brand' }, { key: 'color' }], |
| 144 | + } |
| 145 | + |
| 146 | + const actual = attributeGroupSync.buildActions(now, before) |
| 147 | + const expected = [ |
| 148 | + { action: 'removeAttribute', attribute: before.attributes[0] }, |
| 149 | + { action: 'addAttribute', attribute: now.attributes[0] }, |
| 150 | + { action: 'addAttribute', attribute: now.attributes[2] }, |
| 151 | + ] |
| 152 | + expect(actual).toEqual(expected) |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + describe('Delete first attributes', () => { |
| 157 | + test('should build multiple actions for required changes', () => { |
| 158 | + const before = { |
| 159 | + attributes: [{ key: 'Size' }, { key: 'Brand' }, { key: 'Color' }], |
| 160 | + } |
| 161 | + const now = { |
| 162 | + attributes: [{ key: 'Color' }], |
| 163 | + } |
| 164 | + |
| 165 | + const actual = attributeGroupSync.buildActions(now, before) |
| 166 | + const expected = [ |
| 167 | + { action: 'removeAttribute', attribute: before.attributes[0] }, |
| 168 | + { action: 'removeAttribute', attribute: before.attributes[1] }, |
| 169 | + ] |
| 170 | + expect(actual).toEqual(expected) |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + describe('Delete multiple attributes', () => { |
| 175 | + test('should build multiple actions for required changes', () => { |
| 176 | + const before = { |
| 177 | + attributes: [ |
| 178 | + { key: 'Size' }, |
| 179 | + { key: 'Brand' }, |
| 180 | + { key: 'Quality' }, |
| 181 | + { key: 'Color' }, |
| 182 | + { key: 'Model' }, |
| 183 | + { key: 'attr-1' }, |
| 184 | + ], |
| 185 | + } |
| 186 | + const now = { |
| 187 | + attributes: [ |
| 188 | + { key: 'Brand' }, |
| 189 | + { key: 'Quality' }, |
| 190 | + { key: 'Model' }, |
| 191 | + { key: 'attr-2' }, |
| 192 | + ], |
| 193 | + } |
| 194 | + |
| 195 | + const actual = attributeGroupSync.buildActions(now, before) |
| 196 | + const expected = [ |
| 197 | + { action: 'removeAttribute', attribute: before.attributes[0] }, |
| 198 | + { action: 'removeAttribute', attribute: before.attributes[3] }, |
| 199 | + { action: 'addAttribute', attribute: now.attributes[3] }, |
| 200 | + { action: 'removeAttribute', attribute: before.attributes[5] }, |
| 201 | + ] |
| 202 | + expect(actual).toEqual(expected) |
| 203 | + }) |
| 204 | + }) |
| 205 | +}) |
0 commit comments