Files
faunosteo/apps/app/tests/unit/editor-ids-same.test.ts
T

52 lines
1.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { newElementId } from '@/editor/ids'
import { sameDocument } from '@/editor/core/sameDocument'
describe('newElementId', () => {
it('is the prefix and ten characters of a-z2-7', () => {
for (const prefix of [
'view',
'anno',
'cat',
'ref',
'zone',
'fcat',
'node',
] as const) {
expect(newElementId(prefix)).toMatch(
new RegExp(`^${prefix}-[a-z2-7]{10}$`),
)
}
})
it('does not repeat in a thousand draws', () => {
const ids = new Set(
Array.from({ length: 1000 }, () => newElementId('anno')),
)
expect(ids.size).toBe(1000)
})
})
describe('sameDocument', () => {
it('ignores key order at any depth', () => {
expect(
sameDocument(
'{"a":1,"b":{"c":[1,2],"d":null}}',
'{"b":{"d":null,"c":[1,2]},"a":1}',
),
).toBe(true)
})
it('keeps array order and values significant', () => {
expect(sameDocument('{"a":[1,2]}', '{"a":[2,1]}')).toBe(false)
expect(sameDocument('{"a":1}', '{"a":1,"b":null}')).toBe(false)
expect(sameDocument('{"a":1}', '{"a":"1"}')).toBe(false)
expect(sameDocument('[]', '{}')).toBe(false)
})
it('compares a non-JSON text as a string', () => {
expect(sameDocument('x', 'x')).toBe(true)
expect(sameDocument('x', '{}')).toBe(false)
})
})