forked from bcherny/json-schema-to-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testUtils.ts
48 lines (46 loc) · 1.75 KB
/
testUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import test from 'ava'
import {link} from '../src/linker'
import {LinkedJSONSchema} from '../src/types/JSONSchema'
import {pathTransform, generateName, isSchemaLike} from '../src/utils'
export function run() {
test('pathTransform', t => {
t.is(pathTransform('types', 'schemas', 'schemas/foo/a.json'), 'types/foo')
t.is(pathTransform('./schemas/types', './schemas', 'schemas/foo/bar/a.json'), 'schemas/types/foo/bar')
t.is(pathTransform('types', './src/../types/../schemas', 'schemas/foo/a.json'), 'types/foo')
})
test('generateName', t => {
const usedNames = new Set<string>()
t.is(generateName('a', usedNames), 'A')
t.is(generateName('abc', usedNames), 'Abc')
t.is(generateName('ABcd', usedNames), 'ABcd')
t.is(generateName('$Abc_123', usedNames), '$Abc_123')
t.is(generateName('Abc-de-f', usedNames), 'AbcDeF')
// Index should increment:
t.is(generateName('a', usedNames), 'A1')
t.is(generateName('a', usedNames), 'A2')
t.is(generateName('a', usedNames), 'A3')
})
test('isSchemaLike', t => {
const schema = link({
title: 'Example Schema',
type: 'object',
properties: {
firstName: {
type: 'string',
},
lastName: {
id: 'lastName',
type: 'string',
},
},
required: ['firstName', 'lastName'],
})
t.is(isSchemaLike(schema), true)
t.is(isSchemaLike([] as any as LinkedJSONSchema), false)
t.is(isSchemaLike(schema.properties as LinkedJSONSchema), false)
t.is(isSchemaLike(schema.required as any as LinkedJSONSchema), false)
t.is(isSchemaLike(schema.title as any as LinkedJSONSchema), false)
t.is(isSchemaLike(schema.properties!.firstName), true)
t.is(isSchemaLike(schema.properties!.lastName), true)
})
}