-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(use-data-query): throw more specific error on serialization failure
- Loading branch information
ismay
committed
Aug 12, 2021
1 parent
93938cc
commit 35a6b28
Showing
4 changed files
with
77 additions
and
54 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { stableVariablesHash } from './stableVariablesHash' | ||
|
||
describe('stableVariablesHash', () => { | ||
it('sorts objects before hashing', () => { | ||
const one = { | ||
a: { | ||
one: 1, | ||
two: 2, | ||
three: 3, | ||
}, | ||
b: [1, 2, 3], | ||
c: 'c', | ||
} | ||
const two = { | ||
c: 'c', | ||
b: [1, 2, 3], | ||
a: { | ||
three: 3, | ||
two: 2, | ||
one: 1, | ||
}, | ||
} | ||
|
||
expect(stableVariablesHash(one)).toEqual(stableVariablesHash(two)) | ||
}) | ||
|
||
it('can handle primitives', () => { | ||
const one = undefined | ||
const two = 'string' | ||
const three = 3 | ||
const four = null | ||
const five = true | ||
|
||
expect(stableVariablesHash(one)).toMatchInlineSnapshot(`undefined`) | ||
expect(stableVariablesHash(two)).toMatchInlineSnapshot(`"\\"string\\""`) | ||
expect(stableVariablesHash(three)).toMatchInlineSnapshot(`"3"`) | ||
expect(stableVariablesHash(four)).toMatchInlineSnapshot(`"null"`) | ||
expect(stableVariablesHash(five)).toMatchInlineSnapshot(`"true"`) | ||
}) | ||
|
||
it('throws a clear error when something goes wrong', () => { | ||
const unserializable = { | ||
value: 'value', | ||
} | ||
unserializable.circular = unserializable | ||
|
||
expect(() => | ||
stableVariablesHash(unserializable) | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Could not serialize variables. Make sure that the variables do not contain circular references and can be processed by JSON.stringify."` | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters