Skip to content

Commit c89c228

Browse files
committed
test: fills unserializable
1 parent d2d136f commit c89c228

File tree

3 files changed

+64
-52
lines changed

3 files changed

+64
-52
lines changed

src/core/constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export const FLYTRAP_UNSERIALIZABLE_VALUE = 'FLYTRAP_UNSERIALIZABLE_VALUE'
66
export const FLYTRAP_FUNCTION = 'FLYTRAP_FUNCTION'
77
export const FLYTRAP_DOM_EVENT = 'FLYTRAP_DOM_EVENT'
88
export const FLYTRAP_CIRCULAR = 'FLYTRAP_CIRCULAR'
9+
10+
export const FLYTRAP_REPLACE_VALUES = [
11+
FLYTRAP_UNSERIALIZABLE_VALUE,
12+
FLYTRAP_FUNCTION,
13+
FLYTRAP_DOM_EVENT,
14+
FLYTRAP_CIRCULAR
15+
]

src/core/util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getLoadedConfig } from './config'
2-
import { FLYTRAP_UNSERIALIZABLE_VALUE } from './constants'
2+
import { FLYTRAP_REPLACE_VALUES, FLYTRAP_UNSERIALIZABLE_VALUE } from './constants'
33
import { SourceType } from './types'
44

55
/**
@@ -175,13 +175,14 @@ export function tryCatchSync<DType = unknown, EType = unknown>(
175175
// function fillMissingFlytrapValues(lackingObject: any, fullObject: any) {}
176176

177177
type ReplaceValue = any
178-
type InputValue = ReplaceValue | typeof FLYTRAP_UNSERIALIZABLE_VALUE
178+
type InputValue = ReplaceValue | (typeof FLYTRAP_REPLACE_VALUES)[number]
179179

180180
export function fillUnserializableFlytrapValues(
181181
input: InputValue,
182182
replaceValue: ReplaceValue
183183
): ReplaceValue {
184-
if (input === FLYTRAP_UNSERIALIZABLE_VALUE) {
184+
// if (input === FLYTRAP_UNSERIALIZABLE_VALUE) {
185+
if (FLYTRAP_REPLACE_VALUES.includes(input)) {
185186
if (replaceValue === undefined) throw new Error(`Replace value is undefined.`)
186187
return replaceValue
187188
}

test/args.test.ts

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
import { describe, expect, it } from 'vitest'
22
import { fillUnserializableFlytrapValues } from '../src/core/util'
3-
import { FLYTRAP_UNSERIALIZABLE_VALUE } from '../src/core/constants'
3+
import { FLYTRAP_REPLACE_VALUES, FLYTRAP_UNSERIALIZABLE_VALUE } from '../src/core/constants'
44

55
describe('Replay args', () => {
66
const mockReplaceValue = {
77
hello: 'world'
88
}
99

1010
it('fills strings', () => {
11-
expect(
12-
fillUnserializableFlytrapValues(FLYTRAP_UNSERIALIZABLE_VALUE, 'hello world')
13-
).toStrictEqual('hello world')
11+
FLYTRAP_REPLACE_VALUES.forEach((replaceValue) => {
12+
expect(fillUnserializableFlytrapValues(replaceValue, 'hello world')).toStrictEqual(
13+
'hello world'
14+
)
15+
})
1416
})
1517

1618
it('fills numbers', () => {
17-
expect(fillUnserializableFlytrapValues(FLYTRAP_UNSERIALIZABLE_VALUE, 1)).toStrictEqual(1)
19+
FLYTRAP_REPLACE_VALUES.forEach((replaceValue) => {
20+
expect(fillUnserializableFlytrapValues(replaceValue, 1)).toStrictEqual(1)
21+
})
1822
})
1923

2024
it('fills arrays', () => {
21-
expect(
22-
fillUnserializableFlytrapValues([FLYTRAP_UNSERIALIZABLE_VALUE], [mockReplaceValue])
23-
).toStrictEqual([mockReplaceValue])
24-
expect(
25-
fillUnserializableFlytrapValues(
26-
[FLYTRAP_UNSERIALIZABLE_VALUE, FLYTRAP_UNSERIALIZABLE_VALUE],
27-
[mockReplaceValue, mockReplaceValue]
28-
)
29-
).toStrictEqual([mockReplaceValue, mockReplaceValue])
25+
FLYTRAP_REPLACE_VALUES.forEach((replaceValue) => {
26+
expect(fillUnserializableFlytrapValues([replaceValue], [mockReplaceValue])).toStrictEqual([
27+
mockReplaceValue
28+
])
29+
expect(
30+
fillUnserializableFlytrapValues(
31+
[replaceValue, replaceValue],
32+
[mockReplaceValue, mockReplaceValue]
33+
)
34+
).toStrictEqual([mockReplaceValue, mockReplaceValue])
35+
})
3036
})
3137

3238
it('works with null and undefined', () => {
@@ -39,47 +45,45 @@ describe('Replay args', () => {
3945
})
4046

4147
it('throws when matching keys not same shape', () => {
42-
expect(() =>
43-
fillUnserializableFlytrapValues(
44-
{ hello: [FLYTRAP_UNSERIALIZABLE_VALUE] },
45-
{ hello: mockReplaceValue }
46-
)
47-
).toThrow()
48+
FLYTRAP_REPLACE_VALUES.forEach((replaceValue) => {
49+
expect(() =>
50+
fillUnserializableFlytrapValues({ hello: [replaceValue] }, { hello: mockReplaceValue })
51+
).toThrow()
4852

49-
expect(() =>
50-
fillUnserializableFlytrapValues(
51-
{ hello: [FLYTRAP_UNSERIALIZABLE_VALUE], world: [] },
52-
{ hello: [mockReplaceValue] }
53-
)
54-
).not.toThrow()
53+
expect(() =>
54+
fillUnserializableFlytrapValues(
55+
{ hello: [replaceValue], world: [] },
56+
{ hello: [mockReplaceValue] }
57+
)
58+
).not.toThrow()
5559

56-
expect(() =>
57-
fillUnserializableFlytrapValues(
58-
{ hello: FLYTRAP_UNSERIALIZABLE_VALUE, world: [] },
59-
{ mockReplaceValue }
60-
)
61-
).toThrow()
60+
expect(() =>
61+
fillUnserializableFlytrapValues({ hello: replaceValue, world: [] }, { mockReplaceValue })
62+
).toThrow()
63+
})
6264
})
6365

6466
it('fills objects', () => {
65-
const lackingObj = { hello: FLYTRAP_UNSERIALIZABLE_VALUE, otherKey: '' }
66-
expect(
67-
fillUnserializableFlytrapValues(lackingObj, { hello: mockReplaceValue, otherKey: '' })
68-
).toStrictEqual({
69-
hello: mockReplaceValue,
70-
otherKey: ''
71-
})
67+
FLYTRAP_REPLACE_VALUES.forEach((replaceValue) => {
68+
const lackingObj = { hello: replaceValue, otherKey: '' }
69+
expect(
70+
fillUnserializableFlytrapValues(lackingObj, { hello: mockReplaceValue, otherKey: '' })
71+
).toStrictEqual({
72+
hello: mockReplaceValue,
73+
otherKey: ''
74+
})
7275

73-
const lackingObjDeep = {
74-
hello: { world: { deep: FLYTRAP_UNSERIALIZABLE_VALUE } },
75-
otherKey: ''
76-
}
77-
const fillObjDeep = { hello: { world: { deep: { hello: 'world' } } }, otherKey: '' }
78-
const fillObjDeepWrong = { deep: { hello: 'world' } }
79-
expect(fillUnserializableFlytrapValues(lackingObjDeep, fillObjDeep)).toStrictEqual({
80-
...fillObjDeep,
81-
otherKey: ''
76+
const lackingObjDeep = {
77+
hello: { world: { deep: replaceValue } },
78+
otherKey: ''
79+
}
80+
const fillObjDeep = { hello: { world: { deep: { hello: 'world' } } }, otherKey: '' }
81+
const fillObjDeepWrong = { deep: { hello: 'world' } }
82+
expect(fillUnserializableFlytrapValues(lackingObjDeep, fillObjDeep)).toStrictEqual({
83+
...fillObjDeep,
84+
otherKey: ''
85+
})
86+
expect(() => fillUnserializableFlytrapValues(lackingObjDeep, fillObjDeepWrong)).toThrow()
8287
})
83-
expect(() => fillUnserializableFlytrapValues(lackingObjDeep, fillObjDeepWrong)).toThrow()
8488
})
8589
})

0 commit comments

Comments
 (0)