Skip to content

Commit 2205830

Browse files
committed
test: fix tests
1 parent 252cd75 commit 2205830

File tree

8 files changed

+61
-37
lines changed

8 files changed

+61
-37
lines changed

src/hooks/transform-data/transform-data.hook.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('transformData', () => {
2525

2626
it('context is 2nd param', () => {
2727
let contextParam
28-
transformData((_rec: any, context: any) => {
28+
transformData((_rec: any, { context }: any) => {
2929
contextParam = context
3030
})(hookBefore)
3131
assert.deepEqual(contextParam, hookBefore)
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
import { lowercase } from './lowercase.transformer.js'
22

3+
const options = { context: {} as any, i: 0 }
4+
35
describe('transformers/lowercase', () => {
46
it('single string', () => {
57
const item = { name: 'TEST' }
6-
lowercase('name')(item)
8+
lowercase('name')(item, options)
79
expect(item).toEqual({ name: 'test' })
810
})
911

1012
it('multiple strings', () => {
1113
const item = { name: 'TEST', email: 'TEST@EXAMPLE.COM' }
12-
lowercase(['name', 'email'])(item)
14+
lowercase(['name', 'email'])(item, options)
1315
expect(item).toEqual({ name: 'test', email: 'test@example.com' })
1416
})
1517

1618
it('throws error for non-string values', () => {
1719
const item = { name: 123 }
18-
expect(() => lowercase('name')(item)).toThrow(
20+
expect(() => lowercase('name')(item, options)).toThrow(
1921
"Expected string (lowercase 'name')",
2022
)
2123
})
2224

2325
it('ignores null or undefined values', () => {
2426
const item = { name: null, email: undefined }
25-
lowercase(['name', 'email'])(item)
27+
lowercase(['name', 'email'])(item, options)
2628
expect(item).toEqual({ name: null, email: undefined })
2729
})
2830

2931
it('does not throw if field is missing', () => {
3032
const item = { name: 'Test' }
31-
lowercase('missingField')(item)
33+
lowercase('missingField')(item, options)
3234
expect(item).toEqual({ name: 'Test' })
3335
})
3436

3537
it('handles dot.notation', () => {
3638
const item = { user: { name: 'TEST' } }
37-
lowercase('user.name')(item)
39+
lowercase('user.name')(item, options)
3840
expect(item).toEqual({ user: { name: 'test' } })
3941
})
4042
})

src/transformers/omit/omit.transformer.test.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
import { omit } from './omit.transformer.js'
22

3+
const options = { context: {} as any, i: 0 }
4+
35
describe('transformers/omit', () => {
46
it('single field', () => {
5-
const item = omit('email')({ name: 'John', email: 'john@example.com' })
7+
const item = omit('email')(
8+
{ name: 'John', email: 'john@example.com' },
9+
options,
10+
)
611
expect(item).toEqual({ name: 'John' })
712
})
813

914
it('multiple fields', () => {
10-
const item = omit(['email', 'age'])({
11-
name: 'John',
12-
email: 'john@example.com',
13-
age: 30,
14-
})
15+
const item = omit(['email', 'age'])(
16+
{
17+
name: 'John',
18+
email: 'john@example.com',
19+
age: 30,
20+
},
21+
options,
22+
)
1523
expect(item).toEqual({ name: 'John' })
1624
})
1725

1826
it('does not throw if field is missing', () => {
19-
const item = omit('missingField')({ name: 'John' })
27+
const item = omit('missingField')({ name: 'John' }, options)
2028
expect(item).toEqual({ name: 'John' })
2129
})
2230

2331
it('handles dot notation', () => {
24-
const item = omit('user.email')({
25-
user: { name: 'John', email: 'john@example.com' },
26-
})
32+
const item = omit('user.email')(
33+
{
34+
user: { name: 'John', email: 'john@example.com' },
35+
},
36+
options,
37+
)
2738

2839
expect(item).toEqual({ user: { name: 'John' } })
2940
})

src/transformers/parse-date/parse-date.transformer.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { parseDate } from './parse-date.transformer.js'
22

3+
const options = { context: {} as any, i: 0 }
4+
35
describe('transformers/parseDate', () => {
46
it('single field', () => {
57
const item = { date: '2023-10-01T12:00:00Z' }
6-
parseDate('date')(item)
8+
parseDate('date')(item, options)
79
expect(item).toEqual({ date: new Date('2023-10-01T12:00:00Z') })
810
})
911

@@ -12,7 +14,7 @@ describe('transformers/parseDate', () => {
1214
startDate: '2023-10-01T12:00:00Z',
1315
endDate: '2023-10-02T12:00:00Z',
1416
}
15-
parseDate(['startDate', 'endDate'])(item)
17+
parseDate(['startDate', 'endDate'])(item, options)
1618
expect(item).toEqual({
1719
startDate: new Date('2023-10-01T12:00:00Z'),
1820
endDate: new Date('2023-10-02T12:00:00Z'),
@@ -21,19 +23,19 @@ describe('transformers/parseDate', () => {
2123

2224
it('ignores null or undefined values', () => {
2325
const item = { date: null, anotherDate: undefined }
24-
parseDate(['date', 'anotherDate'])(item)
26+
parseDate(['date', 'anotherDate'])(item, options)
2527
expect(item).toEqual({ date: null, anotherDate: undefined })
2628
})
2729

2830
it('does not throw if field is missing', () => {
2931
const item = { date: '2023-10-01T12:00:00Z' }
30-
parseDate('missingField')(item)
32+
parseDate('missingField')(item, options)
3133
expect(item).toEqual({ date: '2023-10-01T12:00:00Z' })
3234
})
3335

3436
it('handles dot notation', () => {
3537
const item = { event: { startTime: '2023-10-01T12:00:00Z' } }
36-
parseDate('event.startTime')(item)
38+
parseDate('event.startTime')(item, options)
3739
expect(item).toEqual({
3840
event: { startTime: new Date('2023-10-01T12:00:00Z') },
3941
})
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import { pick } from './pick.transformer.js'
22

3+
const options = { context: {} as any, i: 0 }
4+
35
describe('transformers/pick', () => {
46
it('single field', () => {
57
const item = { name: 'John', email: 'john@example.com' }
6-
const picked = pick('email')(item)
8+
const picked = pick('email')(item, options)
79
expect(picked).toEqual({ email: 'john@example.com' })
810
})
911

1012
it('multiple fields', () => {
1113
const item = { name: 'John', email: 'john@example.com', age: 30 }
12-
const picked = pick(['email', 'age'])(item)
14+
const picked = pick(['email', 'age'])(item, options)
1315
expect(picked).toEqual({ email: 'john@example.com', age: 30 })
1416
})
1517

1618
it('does not throw if field is missing', () => {
1719
const item = { name: 'John' }
18-
const picked = pick('missingField')(item)
20+
const picked = pick('missingField')(item, options)
1921
expect(picked).toEqual({})
2022
})
2123

2224
it('handles dot notation', () => {
2325
const item = { user: { name: 'John', email: 'john@example.com' } }
24-
const picked = pick('user.email')(item)
26+
const picked = pick('user.email')(item, options)
2527
expect(picked).toEqual({ user: { email: 'john@example.com' } })
2628
})
2729
})

src/transformers/set-now/set-now.transformer.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { setNow } from './set-now.transformer.js'
22

3+
const options = { context: {} as any, i: 0 }
4+
35
describe('transformers/setNow', () => {
46
it('sets current date on single field', () => {
57
const item = {}
6-
setNow('createdAt')(item)
8+
setNow('createdAt')(item, options)
79
expect(item).toEqual({ createdAt: expect.any(Date) })
810
})
911

1012
it('sets current date on multiple fields', () => {
1113
const item = {}
12-
setNow(['createdAt', 'updatedAt'])(item)
14+
setNow(['createdAt', 'updatedAt'])(item, options)
1315
expect(item).toEqual({
1416
createdAt: expect.any(Date),
1517
updatedAt: expect.any(Date),
@@ -21,7 +23,7 @@ describe('transformers/setNow', () => {
2123
createdAt: new Date('2020-01-01'),
2224
updatedAt: new Date('2020-01-01'),
2325
}
24-
setNow(['createdAt', 'updatedAt'])(item)
26+
setNow(['createdAt', 'updatedAt'])(item, options)
2527
expect(item.createdAt.getFullYear()).toBe(new Date().getFullYear())
2628
expect(item.updatedAt.getFullYear()).toBe(new Date().getFullYear())
2729
})
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
11
import { trim } from './trim.transformer.js'
22

3+
const options = { context: {} as any, i: 0 }
4+
35
describe('transformers/trim', () => {
46
it('single string', () => {
57
const item = { name: ' TEST ' }
6-
trim('name')(item)
8+
trim('name')(item, options)
79
expect(item).toEqual({ name: 'TEST' })
810
})
911

1012
it('multiple strings', () => {
1113
const item = { name: ' TEST ', email: ' TEST@EXAMPLE.COM ' }
12-
trim(['name', 'email'])(item)
14+
trim(['name', 'email'])(item, options)
1315
expect(item).toEqual({ name: 'TEST', email: 'TEST@EXAMPLE.COM' })
1416
})
1517

1618
it('throws error for non-string values', () => {
1719
const item = { name: 123 }
18-
expect(() => trim('name')(item)).toThrow("Expected string (trim 'name')")
20+
expect(() => trim('name')(item, options)).toThrow(
21+
"Expected string (trim 'name')",
22+
)
1923
})
2024

2125
it('ignores null or undefined values', () => {
2226
const item = { name: null, email: undefined }
23-
trim(['name', 'email'])(item)
27+
trim(['name', 'email'])(item, options)
2428
expect(item).toEqual({ name: null, email: undefined })
2529
})
2630

2731
it('does not throw if field is missing', () => {
2832
const item = { name: 'Test' }
29-
trim('missingField')(item)
33+
trim('missingField')(item, options)
3034
expect(item).toEqual({ name: 'Test' })
3135
})
3236

3337
it('handles dot.notation', () => {
3438
const item = { user: { name: ' TEST ' } }
35-
trim('user.name')(item)
39+
trim('user.name')(item, options)
3640
expect(item).toEqual({ user: { name: 'TEST' } })
3741
})
3842
})

test/index.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const hooks = [
2323
'setField',
2424
'setResult',
2525
'setSlug',
26+
'skippable',
2627
'softDelete',
2728
'stashBefore',
2829
'throwIf',
@@ -48,9 +49,8 @@ const utils = [
4849
'iterateFind',
4950
'mutateData',
5051
'mutateResult',
51-
'shouldSkip',
52+
'patchBatch',
5253
'skipResult',
53-
'skippable',
5454
'transformParams',
5555
] satisfies (keyof typeof exportedUtils)[]
5656

@@ -62,6 +62,7 @@ const predicates = [
6262
'some',
6363
'isContext',
6464
'isPaginated',
65+
'shouldSkip',
6566
] satisfies (keyof typeof exportedPredicates)[]
6667

6768
const transformers = [

0 commit comments

Comments
 (0)