From 59ddd98d2c0eac468e8a6923265b285c257e720b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20V=C3=A9zina?= Date: Tue, 25 Apr 2023 16:55:55 -0400 Subject: [PATCH] Add tests for blocks --- src/deserialize.ts | 20 +++++-- test/blocks.test.ts | 117 +++++++++++++++++++++++++++++++++++++++ test/deserialize.test.ts | 10 ++++ 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 test/blocks.test.ts diff --git a/src/deserialize.ts b/src/deserialize.ts index 69d8cc0..8490efe 100644 --- a/src/deserialize.ts +++ b/src/deserialize.ts @@ -32,7 +32,7 @@ export function deserializeMany( result: Type[], store: NormalizedStore, depth = 0, -): Resource[] { +): (Resource | null)[] { return result.map((resource) => deserializeOne(resource, store, depth)) } @@ -40,7 +40,16 @@ export function deserializeOne( result: Type, store: NormalizedStore, depth = 0, -): Resource { +): Resource | null { + if ( + !result.type || + typeof result.id === 'undefined' || + !store[camelCase(result.type)] || + typeof store[camelCase(result.type)][result.id] === 'undefined' + ) { + return null + } + const serializedResource: JsonApiResource = store[camelCase(result.type)][result.id] @@ -55,7 +64,10 @@ export function deserializeOne( const resource: Resource = { ...serializedResource, - attributes: camelCaseKeys(serializedResource.attributes), + } + + if (serializedResource.attributes) { + resource.attributes = camelCaseKeys(serializedResource.attributes) } if (serializedResource.relationships) { @@ -88,7 +100,7 @@ export function deserialize( result: Type[] | Type, store: NormalizedStore, depth = 0, -): Resource | Resource[] { +): (Resource | null) | (Resource | null)[] { if (Array.isArray(result)) { return deserializeMany(result, store, depth) } diff --git a/test/blocks.test.ts b/test/blocks.test.ts new file mode 100644 index 0000000..af1eb05 --- /dev/null +++ b/test/blocks.test.ts @@ -0,0 +1,117 @@ +import { test, expect } from 'vitest' +import { data as jsonData } from './response.test' +import { normalize } from '../src/normalize' +import { deserialize } from '../src/deserialize' +import { blocks } from '../src/helpers/blocks' + +test('blocks', () => { + const blocksRelationship = { + links: { + self: '#', + related: '#', + }, + data: [ + { + type: 'blocks', + id: '1', + }, + { + type: 'blocks', + id: '2', + }, + { + type: 'blocks', + id: '3', + }, + { + type: 'blocks', + id: '4', + }, + ], + meta: { + editors: { + default: ['1', '2', '4'], + extra: ['3'], + }, + }, + } + + const blocksResources: any = [ + { + type: 'blocks', + id: '1', + attributes: { + blockType: 'images', + editorName: 'default', + childKey: null, + position: 1, + content: {}, + }, + }, + { + type: 'blocks', + id: '2', + attributes: { + blockType: 'text', + editorName: 'default', + childKey: null, + position: 2, + content: {}, + }, + }, + { + type: 'blocks', + id: '3', + attributes: { + blockType: 'text', + editorName: 'extra', + childKey: null, + position: 1, + content: {}, + }, + }, + { + type: 'blocks', + id: '4', + attributes: { + blockType: 'gallery', + editorName: 'default', + childKey: null, + position: 3, + content: {}, + }, + }, + ] + + const data = { + ...jsonData, + included: blocksResources, + } + data.data[0].relationships.blocks = blocksRelationship + + const normalized = normalize(data) + const deserialized = deserialize(normalized.result, normalized.resources) + expect(Array.isArray(deserialized)).toBeTruthy() + + const resource = deserialized.pop() + expect(resource.type).toBe('pages') + expect(resource.relationships.blocks.data).toHaveLength(4) + expect(resource.relationships.blocks.meta.editors).toMatchInlineSnapshot(` + { + "default": [ + "1", + "2", + "4", + ], + "extra": [ + "3", + ], + } + `) + + const resourceBlocks = blocks(resource) + expect(Object.keys(resourceBlocks)).toContain('default') + expect(resourceBlocks.default).toHaveLength(3) + expect(Object.keys(resourceBlocks)).toContain('extra') + expect(resourceBlocks.extra).toHaveLength(1) +}) diff --git a/test/deserialize.test.ts b/test/deserialize.test.ts index 05aab1b..7882b50 100644 --- a/test/deserialize.test.ts +++ b/test/deserialize.test.ts @@ -18,4 +18,14 @@ test('deserialize', () => { const blocks = deserialized[0].relationships.blocks expect(blocks.data[0].attributes.blockType).toBe('images') + + const emptyIncluded = { ...data, included: [] } + const normalizedEmptyIncluded = normalize(emptyIncluded) + const deserializedEmptyIncluded = deserialize( + normalizedEmptyIncluded.result, + normalizedEmptyIncluded.resources, + ) + expect(deserializedEmptyIncluded).toHaveLength(1) + expect(deserializedEmptyIncluded[0].relationships.blocks).toBeTypeOf('object') + expect(deserializedEmptyIncluded[0].relationships.blocks.data).toHaveLength(0) })