|
| 1 | +// https://github.com/EnCiv/civil-pursuit/issues/215 |
| 2 | +import { deriveReducedPointList } from '../deliberation-context' |
| 3 | + |
| 4 | +const parentId = '1001' |
| 5 | +const data = { reducedPointList: [] } |
| 6 | +const local = {} |
| 7 | +describe('test deriveReducedPointList', () => { |
| 8 | + test('initially no points in list', () => { |
| 9 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 10 | + expect(reducedPointList).toMatchObject([]) |
| 11 | + }) |
| 12 | + test('called with no changes should give the same ref', () => { |
| 13 | + const oldReducedPointList = data.reducedPointList |
| 14 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 15 | + expect(reducedPointList).toBe(oldReducedPointList) |
| 16 | + }) |
| 17 | + test('called with emtpy data should give the same ref', () => { |
| 18 | + data.pointById = {} |
| 19 | + data.groupIdsLists = [] |
| 20 | + const oldReducedPointList = data.reducedPointList |
| 21 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 22 | + expect(reducedPointList).toBe(oldReducedPointList) |
| 23 | + }) |
| 24 | + test('pointIds but not groupIdsList generates a list', () => { |
| 25 | + const points = [ |
| 26 | + { _id: '1', subject: '1', description: 'describe 1', parentId }, |
| 27 | + { _id: '2', subject: '2', description: 'd2', parentId }, |
| 28 | + { _id: '3', subject: '3', description: 'd3', parentId }, |
| 29 | + { _id: '4', subject: '4', description: 'd4', parentId }, |
| 30 | + { _id: '5', subject: '5', description: 'd5', parentId }, |
| 31 | + { _id: '6', subject: '6', description: 'd6', parentId }, |
| 32 | + ] |
| 33 | + data.pointById = points.reduce((pointById, point) => ((pointById[point._id] = point), pointById), {}) |
| 34 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 35 | + expect(reducedPointList).toMatchObject( |
| 36 | + points.map(point => ({ |
| 37 | + point, |
| 38 | + })) |
| 39 | + ) |
| 40 | + }) |
| 41 | + test('if a point is updated, a new pointById ref is returned, but unchanged points have unchanged refs', () => { |
| 42 | + const oldReducedPointList = deriveReducedPointList(data, local).reducedPointList |
| 43 | + const oldPointRefs = Object.values(data.pointById) |
| 44 | + data.pointById['1'] = { _id: '1', subject: '1', description: 'updated d1', parentId } |
| 45 | + data.pointById = { ...data.pointById } // needs a new ref because it changed |
| 46 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 47 | + expect(reducedPointList).not.toBe(oldReducedPointList) |
| 48 | + expect(reducedPointList[0].point).not.toBe(oldPointRefs[0]) |
| 49 | + for (let i = 1; i <= 5; i++) { |
| 50 | + expect(reducedPointList[i].point).toBe(oldPointRefs[i]) |
| 51 | + } |
| 52 | + }) |
| 53 | + test('grouping points', () => { |
| 54 | + const oldReducedPointList = deriveReducedPointList(data, local).reducedPointList |
| 55 | + data.groupIdsLists = [ |
| 56 | + ['1', '2'], |
| 57 | + ['3', '4', '5'], |
| 58 | + ] |
| 59 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 60 | + expect(reducedPointList).not.toBe(oldReducedPointList) |
| 61 | + expect(reducedPointList).toEqual([ |
| 62 | + { |
| 63 | + point: { _id: '1', subject: '1', description: 'updated d1', parentId }, |
| 64 | + group: [{ _id: '2', subject: '2', description: 'd2', parentId }], |
| 65 | + }, |
| 66 | + { |
| 67 | + point: { _id: '3', subject: '3', description: 'd3', parentId }, |
| 68 | + group: [ |
| 69 | + { _id: '4', subject: '4', description: 'd4', parentId }, |
| 70 | + { _id: '5', subject: '5', description: 'd5', parentId }, |
| 71 | + ], |
| 72 | + }, |
| 73 | + { point: { _id: '6', subject: '6', description: 'd6', parentId } }, |
| 74 | + ]) |
| 75 | + }) |
| 76 | + test('grouping can change', () => { |
| 77 | + const oldReducedPointList = deriveReducedPointList(data, local).reducedPointList |
| 78 | + const oldLastPoint = oldReducedPointList[2] |
| 79 | + data.groupIdsLists = [ |
| 80 | + ['1', '2', '4'], |
| 81 | + ['3', '5'], |
| 82 | + ] |
| 83 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 84 | + expect(reducedPointList).not.toBe(oldReducedPointList) |
| 85 | + expect(reducedPointList).toEqual([ |
| 86 | + { |
| 87 | + point: { _id: '1', subject: '1', description: 'updated d1', parentId }, |
| 88 | + group: [ |
| 89 | + { _id: '2', subject: '2', description: 'd2', parentId }, |
| 90 | + { _id: '4', subject: '4', description: 'd4', parentId }, |
| 91 | + ], |
| 92 | + }, |
| 93 | + { |
| 94 | + point: { _id: '3', subject: '3', description: 'd3', parentId }, |
| 95 | + group: [{ _id: '5', subject: '5', description: 'd5', parentId }], |
| 96 | + }, |
| 97 | + { point: { _id: '6', subject: '6', description: 'd6', parentId } }, |
| 98 | + ]) |
| 99 | + expect(oldLastPoint).toBe(reducedPointList[2]) |
| 100 | + }) |
| 101 | + test('a point in a group is updated', () => { |
| 102 | + const oldReducedPointList = deriveReducedPointList(data, local).reducedPointList |
| 103 | + const oldLastPoint = oldReducedPointList[2] |
| 104 | + data.pointById['2'] = { _id: '2', subject: '2 child of 1', description: 'updated d2 in group', parentId } |
| 105 | + data.pointById = { ...data.pointById } // needs a new ref |
| 106 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 107 | + expect(reducedPointList).not.toBe(oldReducedPointList) |
| 108 | + expect(reducedPointList).toEqual([ |
| 109 | + { |
| 110 | + point: { _id: '1', subject: '1', description: 'updated d1', parentId }, |
| 111 | + group: [ |
| 112 | + { _id: '2', subject: '2 child of 1', description: 'updated d2 in group', parentId }, |
| 113 | + { _id: '4', subject: '4', description: 'd4', parentId }, |
| 114 | + ], |
| 115 | + }, |
| 116 | + { |
| 117 | + point: { _id: '3', subject: '3', description: 'd3', parentId }, |
| 118 | + group: [{ _id: '5', subject: '5', description: 'd5', parentId }], |
| 119 | + }, |
| 120 | + { point: { _id: '6', subject: '6', description: 'd6', parentId } }, |
| 121 | + ]) |
| 122 | + expect(oldLastPoint).toBe(reducedPointList[2]) |
| 123 | + }) |
| 124 | + test('can be ungrouped', () => { |
| 125 | + const oldReducedPointList = deriveReducedPointList(data, local).reducedPointList |
| 126 | + const oldFirstPoint = oldReducedPointList[0] |
| 127 | + data.groupIdsLists = [['1', '2', '4']] |
| 128 | + const { reducedPointList } = deriveReducedPointList(data, local) |
| 129 | + expect(reducedPointList).not.toBe(oldReducedPointList) |
| 130 | + expect(reducedPointList).toEqual([ |
| 131 | + { |
| 132 | + point: { _id: '1', subject: '1', description: 'updated d1', parentId }, |
| 133 | + group: [ |
| 134 | + { _id: '2', subject: '2 child of 1', description: 'updated d2 in group', parentId }, |
| 135 | + { _id: '4', subject: '4', description: 'd4', parentId }, |
| 136 | + ], |
| 137 | + }, |
| 138 | + { |
| 139 | + point: { _id: '3', subject: '3', description: 'd3', parentId }, |
| 140 | + }, |
| 141 | + { point: { _id: '5', subject: '5', description: 'd5', parentId } }, |
| 142 | + { point: { _id: '6', subject: '6', description: 'd6', parentId } }, |
| 143 | + ]) |
| 144 | + expect(oldFirstPoint).toBe(reducedPointList[0]) |
| 145 | + }) |
| 146 | +}) |
0 commit comments