Skip to content

Commit

Permalink
test(lens): parseAtoms cases
Browse files Browse the repository at this point in the history
  • Loading branch information
BANOnotIT committed Oct 19, 2023
1 parent 3c6e8bc commit ccf3897
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/lens/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { test } from 'uvu'
import * as assert from 'uvu/assert'

import './match.test'
import './parseAtoms.test'

import {
combine,
Expand Down
142 changes: 142 additions & 0 deletions packages/lens/src/parseAtoms.test.ts
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()

0 comments on commit ccf3897

Please sign in to comment.