-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEVXP-2603: chore(next): use json schema test suite (#121)
Co-authored-by: Capelo <[email protected]>
- Loading branch information
1 parent
07c7ea1
commit 413b077
Showing
3 changed files
with
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "next/json-schema-test-suite"] | ||
path = next/json-schema-test-suite | ||
url = [email protected]:json-schema-org/JSON-Schema-Test-Suite.git |
Submodule json-schema-test-suite
added at
e52450
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,51 @@ | ||
import type { JsfSchema, SchemaValue } from '../../src/types' | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import util from 'node:util' | ||
import { describe, expect, it } from '@jest/globals' | ||
import { createHeadlessForm } from '../../src' | ||
|
||
interface Test { | ||
description: string | ||
data: SchemaValue | ||
valid: boolean | ||
} | ||
|
||
interface TestSchema { | ||
description: string | ||
schema: JsfSchema | ||
tests: Test[] | ||
} | ||
|
||
expect.extend({ | ||
toBeValid(received: JsfSchema, value: SchemaValue, valid: boolean = true) { | ||
const form = createHeadlessForm(received, { initialValues: value }) | ||
const validationResult = form.handleValidation(value) | ||
const hasFormErrors = validationResult.formErrors !== undefined | ||
const pass = hasFormErrors !== valid | ||
return { | ||
pass, | ||
message: () => `expected ${util.inspect(value)} ${valid ? 'to' : 'not to'} be valid for ${util.inspect(received)}`, | ||
} | ||
}, | ||
}) | ||
|
||
describe.skip('JSON Schema Test Suite', () => { | ||
const testsDir = path.join(__dirname, '..', '..', 'json-schema-test-suite', 'tests', 'draft2020-12') | ||
const testFiles = fs.readdirSync(testsDir).filter(file => file.endsWith('.json')) | ||
|
||
for (const file of testFiles) { | ||
const testFile: TestSchema[] = JSON.parse(fs.readFileSync(path.join(testsDir, file), 'utf8')) | ||
|
||
for (const testSchema of testFile) { | ||
describe(testSchema.description, () => { | ||
for (const test of testSchema.tests) { | ||
it(test.description, () => { | ||
// TODO: properly extend the expect interface | ||
expect(testSchema.schema).toBeValid(test.data, test.valid) | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
}) |