Skip to content

Commit

Permalink
test(lens): parseAtoms cases (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
BANOnotIT authored Oct 20, 2023
1 parent 3c6e8bc commit f870fe5
Show file tree
Hide file tree
Showing 2 changed files with 155 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
154 changes: 154 additions & 0 deletions packages/lens/src/parseAtoms.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
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 spy if inside atom', () => {
const ctx = createTestCtx()

const valueAtom = atom('default')
const parsedAtom = atom((ctx) => parseAtoms(ctx, { key: valueAtom }))

assert.equal(ctx.get(parsedAtom), { key: 'default' })

valueAtom(ctx, 'new')
assert.equal(ctx.get(parsedAtom), { key: 'new' })
;`👍` //?
})

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()

1 comment on commit f870fe5

@vercel
Copy link

@vercel vercel bot commented on f870fe5 Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.