|
1 | 1 | import { describe, expect, test } from 'vitest'
|
2 |
| -import { assertTypes, deepClone, deepMerge, resetModules, toArray } from '../../../packages/vitest/src/utils' |
| 2 | +import { assertTypes, deepClone, deepMerge, objectAttr, resetModules, toArray } from '../../../packages/vitest/src/utils' |
3 | 3 | import { deepMergeSnapshot } from '../../../packages/vitest/src/integrations/snapshot/port/utils'
|
4 | 4 | import type { ModuleCacheMap } from '../../../packages/vite-node/src/types'
|
5 | 5 |
|
@@ -182,3 +182,28 @@ describe('resetModules doesn\'t resets only user modules', () => {
|
182 | 182 | expect(moduleCache.size).toBe(2)
|
183 | 183 | })
|
184 | 184 | })
|
| 185 | + |
| 186 | +describe('objectAttr', () => { |
| 187 | + const arrow = (a: number) => a * 3 |
| 188 | + const func = function (a: number) { |
| 189 | + return a * 3 |
| 190 | + } |
| 191 | + |
| 192 | + test.each` |
| 193 | + value | path | expected |
| 194 | + ${{ foo: 'bar' }} | ${'foo'} | ${'bar'} |
| 195 | + ${{ foo: { bar: 'baz' } }} | ${'foo'} | ${{ bar: 'baz' }} |
| 196 | + ${{ foo: { bar: 'baz' } }} | ${'foo.bar'} | ${'baz'} |
| 197 | + ${{ foo: [{ bar: 'baz' }] }} | ${'foo.0.bar'} | ${'baz'} |
| 198 | + ${{ foo: [1, 2, ['a']] }} | ${'foo'} | ${[1, 2, ['a']]} |
| 199 | + ${{ foo: [1, 2, ['a']] }} | ${'foo.2'} | ${['a']} |
| 200 | + ${{ foo: [1, 2, ['a']] }} | ${'foo.2.0'} | ${'a'} |
| 201 | + ${{ foo: [[1]] }} | ${'foo.0.0'} | ${1} |
| 202 | + ${{ deep: [[[1]]] }} | ${'deep.0.0.0'} | ${1} |
| 203 | + ${{ a: 1, b: 2, c: 3, d: 4 }} | ${'a'} | ${1} |
| 204 | + ${{ arrow }} | ${'arrow'} | ${arrow} |
| 205 | + ${{ func }} | ${'func'} | ${func} |
| 206 | + `('objectAttr($value, $path) -> $expected', ({ value, path, expected }) => { |
| 207 | + expect(objectAttr(value, path)).toEqual(expected) |
| 208 | + }) |
| 209 | +}) |
0 commit comments