-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { createTestCtx } from '@reatom/testing' | ||
import { suite } from 'uvu' | ||
import * as assert from 'uvu/assert' | ||
import { atom } from '@reatom/core' | ||
import { parseAtoms } from './parseAtoms' | ||
|
||
const test = suite('parseAtoms') | ||
|
||
test('should return value', () => { | ||
const ctx = createTestCtx() | ||
|
||
assert.is(parseAtoms(ctx, 'some bare value'), 'some bare value') | ||
assert.is(parseAtoms(ctx, 10), 10) | ||
assert.is( | ||
parseAtoms(ctx, Symbol.for('specialSymbol')), | ||
Symbol.for('specialSymbol'), | ||
) | ||
;`π` //? | ||
}) | ||
|
||
test('should parse deep atoms', () => { | ||
const ctx = createTestCtx() | ||
|
||
assert.is( | ||
parseAtoms( | ||
ctx, | ||
atom(() => atom('deep')), | ||
), | ||
'deep', | ||
) | ||
assert.equal( | ||
parseAtoms( | ||
ctx, | ||
atom(() => [atom(['deep'])]), | ||
), | ||
[['deep']], | ||
) | ||
;`π` //? | ||
}) | ||
|
||
test('should parse records', () => { | ||
const ctx = createTestCtx() | ||
|
||
assert.equal( | ||
parseAtoms(ctx, { | ||
someValue: atom(1), | ||
someDeep: { | ||
deep: { | ||
deep: atom('value'), | ||
}, | ||
}, | ||
}), | ||
{ | ||
someValue: 1, | ||
someDeep: { | ||
deep: { | ||
deep: 'value', | ||
}, | ||
}, | ||
}, | ||
) | ||
;`π` //? | ||
}) | ||
|
||
test('should parse maps', () => { | ||
const ctx = createTestCtx() | ||
|
||
const atomized = new Map() | ||
const keyObj = {} | ||
const keyAtom = atom('') | ||
atomized.set(1, atom(1)) | ||
atomized.set(keyObj, atom({ someKey: atom('someValue') })) | ||
atomized.set(keyAtom, 'someRawValue') | ||
|
||
const parsed = parseAtoms(ctx, atomized) | ||
assert.is(parsed.get(1), 1) | ||
assert.equal(parsed.get(keyObj), { someKey: 'someValue' }) | ||
assert.equal(parsed.get(keyAtom), 'someRawValue') | ||
assert.is(parsed.size, 3) | ||
;`π` //? | ||
}) | ||
|
||
test('should parse sets', () => { | ||
const ctx = createTestCtx() | ||
|
||
const atomized = new Set() | ||
const symbol = Symbol() | ||
const keyObj = { __id__: symbol } | ||
atomized.add(atom(1)) | ||
atomized.add(atom(1)) | ||
atomized.add(atom(1)) | ||
atomized.add(atom(1)) | ||
|
||
atomized.add(keyObj) | ||
atomized.add('someRawValue') | ||
|
||
const parsed = parseAtoms(ctx, atomized) | ||
const values = Array.from(parsed.values()) | ||
assert.ok(parsed.has(1), '') | ||
assert.ok(parsed.has('someRawValue')) | ||
|
||
assert.not.ok(parsed.has(keyObj)) | ||
assert.ok(values.some((a: any) => a?.__id__ === symbol)) | ||
|
||
// assert.is(parsed.size, 3) | ||
;`π` //? | ||
}) | ||
|
||
test('should parse mixed values', () => { | ||
const ctx = createTestCtx() | ||
|
||
assert.equal( | ||
parseAtoms(ctx, { | ||
someValue: atom(1), | ||
someDeep: { | ||
deep: { | ||
deep: atom('value'), | ||
}, | ||
}, | ||
}), | ||
{ | ||
someValue: 1, | ||
someDeep: { | ||
deep: { | ||
deep: 'value', | ||
}, | ||
}, | ||
}, | ||
) | ||
;`π` //? | ||
}) | ||
|
||
test('should parse deep structures', () => { | ||
const ctx = createTestCtx() | ||
|
||
assert.equal(parseAtoms(ctx, [[[[[atom('deepStruct')]]]]]), [ | ||
[[[['deepStruct']]]], | ||
]) | ||
;`π` //? | ||
}) | ||
|
||
test.run() |