Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that resolveResponse does not mutate input #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/unit/deep-freeze.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#examples
export function deepFreeze(object) {
// Retrieve the property names defined on object
const propNames = Reflect.ownKeys(object)

// Freeze properties before freezing self
for (const name of propNames) {
const value = object[name]

if ((value && typeof value === 'object') || typeof value === 'function') {
deepFreeze(value)
}
}

return Object.freeze(object)
}
25 changes: 17 additions & 8 deletions test/unit/index-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { deepEqual, notEqual, equal, notDeepEqual } = require('chai').assert
const { deepEqual, notEqual, equal, notDeepEqual, doesNotThrow } = require('chai').assert
const resolveResponse = require('../../index')
const { deepFreeze } = require('./deep-freeze')
const copy = require('fast-copy')

describe('Resolve a', function () {
it('empty response which returns an empty response', function () {
Expand Down Expand Up @@ -493,8 +495,8 @@ describe('Resolve a', function () {
equal(resolved[0].fields.linkfield[2].fields.linkfield[0].sys.id, 'Y', 'sub link id')
})

it('response with links should resolve complex references between items and includes', function () {
const items = [
const example = {
items: [
{
sys: {
type: 'Entry',
Expand Down Expand Up @@ -552,8 +554,8 @@ describe('Resolve a', function () {
},
},
},
]
const includes = {
],
includes: {
Animal: [
{
sys: {
Expand Down Expand Up @@ -619,9 +621,11 @@ describe('Resolve a', function () {
fields: { name: 'Aussie Parrot' },
},
],
}

const resolved = resolveResponse({ items, includes })
},
}
it('response with links should resolve complex references between items and includes', function () {
const resolved = resolveResponse(example)
const { includes } = example
deepEqual(resolved[0].fields.animal.sys, includes.Animal[0].sys, 'pig')
deepEqual(resolved[0].fields.animal.fields.friend.sys, includes.Animal[1].sys, 'groundhog')
deepEqual(resolved[0].fields.anotheranimal.sys.type, 'Link', 'first middle parrot not resolved')
Expand Down Expand Up @@ -1044,4 +1048,9 @@ describe('Resolve a', function () {
const resolved = resolveResponse({ items, includes })
deepEqual(resolved, items)
})

it('does not mutate input', function () {
const frozenCopy = deepFreeze(copy(example))
doesNotThrow(() => resolveResponse(frozenCopy))
})
})