Skip to content

Commit

Permalink
Merge pull request #734 from Financial-Times/apaleslimghost-patch-1
Browse files Browse the repository at this point in the history
fix: improve serialiser error messages
  • Loading branch information
apaleslimghost authored Aug 10, 2023
2 parents d500755 + 21f2541 commit 1cd8f8e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions components/x-interaction/__tests__/serialiser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ describe('serialiser', () => {
it('throws Error if component is not registered in addData', () => {
jest.spyOn(registerComponent, 'getComponent').mockReturnValue(undefined)

expect(() => serialiser.addData('id', Component, {})).toThrow(
`a Serialiser's addData was called for an unregistered component. ensure you're registering your component before attempting to output the hydration data`
expect(() => serialiser.addData('id', Component, {})).toThrowErrorMatchingInlineSnapshot(
`"a Serialiser's addData was called for an unregistered testComponent component with id undefined. ensure you're registering your component before attempting to output the hydration data"`
)
})

it('throws Error if serialiser is destroyed in addData', () => {
serialiser.destroyed = true
jest.spyOn(registerComponent, 'getComponent').mockReturnValue(Component)

expect(() => serialiser.addData('id', Component, {})).toThrow(
`an interaction component was rendered after flushHydrationData was called. ensure you're outputting the hydration data after rendering every component`
expect(() => serialiser.addData('id', Component, {})).toThrowErrorMatchingInlineSnapshot(
`"a testComponent component was rendered after flushHydrationData was called. ensure you're outputting the hydration data after rendering every component"`
)
})

it('throws Error if serialiser is destroyed in flushHydrationData', () => {
serialiser.destroyed = true
expect(() => serialiser.flushHydrationData()).toThrow(
`a Serialiser's flushHydrationData was called twice. ensure you're not reusing a Serialiser between requests`
expect(() => serialiser.flushHydrationData()).toThrowErrorMatchingInlineSnapshot(
`"a Serialiser's flushHydrationData was called twice. ensure you're not reusing a Serialiser between requests"`
)
})

Expand Down
4 changes: 2 additions & 2 deletions components/x-interaction/src/concerns/serialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export class Serialiser {

if (!registeredComponent) {
throw new Error(
`a Serialiser's addData was called for an unregistered component. ensure you're registering your component before attempting to output the hydration data`
`a Serialiser's addData was called for an unregistered ${getComponentName(Component)} component with id ${id}. ensure you're registering your component before attempting to output the hydration data`
)
}

if (this.destroyed) {
throw new Error(
`an interaction component was rendered after flushHydrationData was called. ensure you're outputting the hydration data after rendering every component`
`a ${getComponentName(Component)} component was rendered after flushHydrationData was called. ensure you're outputting the hydration data after rendering every component`
)
}

Expand Down

0 comments on commit 1cd8f8e

Please sign in to comment.