Skip to content

Commit 07bb567

Browse files
committed
test
1 parent cc70128 commit 07bb567

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

lib/utils/fns/index.spec.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect } from 'vitest';
22

3-
import { groupBy, objectEntries, objectValues, find, sprintf, keyBy } from '.'
3+
import { groupBy, objectEntries, objectValues, find, sprintf, keyBy, assignBy } from '.'
44

55
describe('utils', () => {
66
describe('groupBy', () => {
@@ -76,6 +76,78 @@ describe('utils', () => {
7676
})
7777
})
7878

79+
describe('assignBy', () => {
80+
it('should assign array elements to an object using the specified key', () => {
81+
const input = [
82+
{ key: 'foo', firstName: 'jordan', lastName: 'foo' },
83+
{ key: 'bar', firstName: 'jordan', lastName: 'bar' },
84+
{ key: 'baz', firstName: 'james', lastName: 'foxy' },
85+
]
86+
const base = {}
87+
88+
assignBy(input, 'key', base)
89+
90+
expect(base).toEqual({
91+
foo: { key: 'foo', firstName: 'jordan', lastName: 'foo' },
92+
bar: { key: 'bar', firstName: 'jordan', lastName: 'bar' },
93+
baz: { key: 'baz', firstName: 'james', lastName: 'foxy' },
94+
})
95+
})
96+
97+
it('should append to an existing object', () => {
98+
const input = [
99+
{ key: 'foo', firstName: 'jordan', lastName: 'foo' },
100+
{ key: 'bar', firstName: 'jordan', lastName: 'bar' },
101+
]
102+
const base: any = { existing: 'value' }
103+
104+
assignBy(input, 'key', base)
105+
106+
expect(base).toEqual({
107+
existing: 'value',
108+
foo: { key: 'foo', firstName: 'jordan', lastName: 'foo' },
109+
bar: { key: 'bar', firstName: 'jordan', lastName: 'bar' },
110+
})
111+
})
112+
113+
it('should handle empty array', () => {
114+
const base: any = { existing: 'value' }
115+
116+
assignBy([], 'key', base)
117+
118+
expect(base).toEqual({ existing: 'value' })
119+
})
120+
121+
it('should handle null/undefined array', () => {
122+
const base: any = { existing: 'value' }
123+
124+
assignBy(null as any, 'key', base)
125+
expect(base).toEqual({ existing: 'value' })
126+
127+
assignBy(undefined as any, 'key', base)
128+
expect(base).toEqual({ existing: 'value' })
129+
})
130+
131+
it('should override existing values with the same key', () => {
132+
const input = [
133+
{ key: 'foo', firstName: 'jordan', lastName: 'updated' },
134+
{ key: 'bar', firstName: 'james', lastName: 'new' },
135+
]
136+
const base: any = {
137+
foo: { key: 'foo', firstName: 'john', lastName: 'original' },
138+
existing: 'value'
139+
}
140+
141+
assignBy(input, 'key', base)
142+
143+
expect(base).toEqual({
144+
existing: 'value',
145+
foo: { key: 'foo', firstName: 'jordan', lastName: 'updated' },
146+
bar: { key: 'bar', firstName: 'james', lastName: 'new' },
147+
})
148+
})
149+
})
150+
79151
describe('sprintf', () => {
80152
it('sprintf(msg)', () => {
81153
expect(sprintf('this is my message')).toBe('this is my message')

0 commit comments

Comments
 (0)