Skip to content

Commit

Permalink
Add tests for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrckvzn committed Apr 25, 2023
1 parent 3954089 commit 59ddd98
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,24 @@ export function deserializeMany<Type extends { type: string; id: ID }>(
result: Type[],
store: NormalizedStore,
depth = 0,
): Resource[] {
): (Resource | null)[] {
return result.map((resource) => deserializeOne(resource, store, depth))
}

export function deserializeOne<Type extends { type: string; id: ID }>(
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]

Expand All @@ -55,7 +64,10 @@ export function deserializeOne<Type extends { type: string; id: ID }>(

const resource: Resource = {
...serializedResource,
attributes: camelCaseKeys(serializedResource.attributes),
}

if (serializedResource.attributes) {
resource.attributes = camelCaseKeys(serializedResource.attributes)
}

if (serializedResource.relationships) {
Expand Down Expand Up @@ -88,7 +100,7 @@ export function deserialize<Type extends { type: string; id: ID }>(
result: Type[] | Type,
store: NormalizedStore,
depth = 0,
): Resource | Resource[] {
): (Resource | null) | (Resource | null)[] {
if (Array.isArray(result)) {
return deserializeMany(result, store, depth)
}
Expand Down
117 changes: 117 additions & 0 deletions test/blocks.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
10 changes: 10 additions & 0 deletions test/deserialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 59ddd98

Please sign in to comment.