|
| 1 | +const COLORS = ['red', 'white', 'black', 'pink', 'green', 'blue', 'yellow', 'orange', 'green', 'teal']; |
| 2 | +const SIZES = ['square', 'rectangle', 'circle', 'oval', 'cube', 'small', 'medium', 'large', 'extra large']; |
| 3 | +const MAKES = ['suv', 'sedan', 'minivan', 'electric', 'hybrid', 'truck', 'sport']; |
| 4 | + |
| 5 | +let FIXTURE_ID = 0; |
| 6 | + |
| 7 | +type JSONIdentifier = { id: string; type: string }; |
| 8 | + |
| 9 | +type JSONAPIResource = { |
| 10 | + id: string; |
| 11 | + type: string; |
| 12 | + attributes: Record<string, string>; |
| 13 | + relationships?: Record<string, { data: JSONIdentifier | JSONIdentifier[] }>; |
| 14 | +}; |
| 15 | + |
| 16 | +type JSONAPIPayload = { |
| 17 | + data: JSONAPIResource[]; |
| 18 | + included: JSONAPIResource[]; |
| 19 | +}; |
| 20 | + |
| 21 | +function getIndex(index: number, fixtures: unknown[]) { |
| 22 | + const count = fixtures.length; |
| 23 | + return index % count; |
| 24 | +} |
| 25 | + |
| 26 | +function assignToMany(resource: JSONAPIResource, id: string) { |
| 27 | + resource.relationships = resource.relationships || {}; |
| 28 | + const cars = (resource.relationships.cars = resource.relationships.cars || { data: [] }); |
| 29 | + assert('Expected cars.data to be an array', Array.isArray(cars.data)); |
| 30 | + cars.data.push({ |
| 31 | + type: 'car', |
| 32 | + id, |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function getRelatedResource(fixtures: JSONAPIResource[], index: number, id: string) { |
| 37 | + const resource = fixtures[getIndex(index, fixtures)]; |
| 38 | + assignToMany(resource, id); |
| 39 | + return { id: resource.id, type: resource.type }; |
| 40 | +} |
| 41 | + |
| 42 | +function createCarsPayload(n: number, c = 1): JSONAPIPayload { |
| 43 | + const colors = getColorResources(c); |
| 44 | + const makes = getMakeResources(); |
| 45 | + const sizes = getSizeResources(); |
| 46 | + const data = new Array<JSONAPIResource>(n); |
| 47 | + for (let i = 0; i < n; i++) { |
| 48 | + const id = `urn:car:${FIXTURE_ID++}`; |
| 49 | + data[i] = { |
| 50 | + id, |
| 51 | + type: 'car', |
| 52 | + attributes: {}, |
| 53 | + relationships: { |
| 54 | + make: { |
| 55 | + data: getRelatedResource(makes, i, id), |
| 56 | + }, |
| 57 | + size: { |
| 58 | + data: getRelatedResource(sizes, i, id), |
| 59 | + }, |
| 60 | + colors: { |
| 61 | + data: |
| 62 | + c === 1 |
| 63 | + ? [ |
| 64 | + getRelatedResource(colors, i, id), |
| 65 | + getRelatedResource(colors, i + 1, id), |
| 66 | + getRelatedResource(colors, i + 2, id), |
| 67 | + ] |
| 68 | + : new Array(colors.length).fill(null).map((_v, ii) => getRelatedResource(colors, i + ii, id)), |
| 69 | + }, |
| 70 | + }, |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + const fixture = { |
| 75 | + data, |
| 76 | + included: ([] as JSONAPIResource[]).concat(colors, makes, sizes), |
| 77 | + }; |
| 78 | + |
| 79 | + return fixture; |
| 80 | +} |
| 81 | + |
| 82 | +function getColorResources(c: number) { |
| 83 | + return COLORS.flatMap((name) => { |
| 84 | + if (c > 1) { |
| 85 | + return new Array(c) |
| 86 | + .fill(null) |
| 87 | + .map((_v, i) => createJsonApiResource(`urn:color:${FIXTURE_ID++}`, 'color', { name: `${name}-${i}` })); |
| 88 | + } else { |
| 89 | + return [createJsonApiResource(`urn:color:${FIXTURE_ID++}`, 'color', { name })]; |
| 90 | + } |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +function getSizeResources() { |
| 95 | + return SIZES.map((name) => createJsonApiResource(`urn:size:${FIXTURE_ID++}`, 'size', { name })); |
| 96 | +} |
| 97 | + |
| 98 | +function getMakeResources() { |
| 99 | + return MAKES.map((name) => createJsonApiResource(`urn:make:${FIXTURE_ID++}`, 'make', { name })); |
| 100 | +} |
| 101 | + |
| 102 | +function createJsonApiResource(id: string, type: string, attributes: Record<string, string>): JSONAPIResource { |
| 103 | + return { |
| 104 | + id, |
| 105 | + type, |
| 106 | + attributes, |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +function deleteHalfTheColors(payload: JSONAPIPayload) { |
| 111 | + const payloadWithRemoval = structuredClone(payload); |
| 112 | + |
| 113 | + for (const carDatum of payloadWithRemoval.data) { |
| 114 | + assert('Expected carDatum to have relationships', carDatum.relationships); |
| 115 | + assert('Expected carDatum to have colors array', Array.isArray(carDatum.relationships.colors.data)); |
| 116 | + const colorsLength = carDatum.relationships.colors.data.length; |
| 117 | + const removedColors = carDatum.relationships.colors.data.splice(0, colorsLength / 2); |
| 118 | + for (const removed of removedColors) { |
| 119 | + const included = payloadWithRemoval.included.find((r) => r.type === 'color' && r.id === removed.id); |
| 120 | + assert('Expected to find color in included', included); |
| 121 | + assert('Expected color to have relationships', included.relationships); |
| 122 | + assert('Expected color to have cars', Array.isArray(included.relationships.cars.data)); |
| 123 | + included.relationships.cars.data = included.relationships.cars.data.filter((car) => car.id !== carDatum.id); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return payloadWithRemoval; |
| 128 | +} |
| 129 | + |
| 130 | +function assert(message: string, condition: unknown): asserts condition { |
| 131 | + if (!condition) { |
| 132 | + throw new Error(`Assertion failed: ${message}`); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +module.exports = { createCarsPayload, deleteHalfTheColors }; |
0 commit comments