Skip to content

Commit

Permalink
test(schema): refactor validation test to use main function
Browse files Browse the repository at this point in the history
Not sure why this test file was initally set up like this,
but it made adding more tests a bit more involved than it
should be.

I also discovered that by using the main validaton
function there was an additional problem item added
to the block test. I have also asserted this in the test.
  • Loading branch information
skogsmaskin committed Sep 6, 2023
1 parent 0753806 commit b3b365e
Showing 1 changed file with 7 additions and 44 deletions.
51 changes: 7 additions & 44 deletions packages/@sanity/schema/test/validation/validation.test.ts
@@ -1,12 +1,5 @@
import {flatten} from 'lodash'
import {traverseSchema} from '../../src/core/traverseSchema'
import {validateSchema} from '../../src/sanity/validateSchema'
import array from '../../src/sanity/validation/types/array'
import block from '../../src/sanity/validation/types/block'
import file from '../../src/sanity/validation/types/file'
import image from '../../src/sanity/validation/types/image'
import object from '../../src/sanity/validation/types/object'
import reference from '../../src/sanity/validation/types/reference'

describe('Validation test', () => {
test('assigns/populates `_problems` property', () => {
Expand All @@ -27,21 +20,7 @@ describe('Validation test', () => {
},
]

const coreTypes = [
{name: 'array', type: 'type'},
{name: 'string', type: 'type'},
{name: 'object', type: 'type'},
]

const visitors = {
array: {visit: array},
object: {visit: object},
}

const validation = traverseSchema(schemaDef, coreTypes, (typeDef, visitorContext) => {
const visitor = visitors[typeDef.type]
return visitor ? visitor.visit(typeDef, visitorContext) : typeDef
})
const validation = validateSchema(schemaDef)

const myArray = validation.get('myArray')
expect(myArray._problems.length).toBeGreaterThan(0)
Expand Down Expand Up @@ -150,28 +129,8 @@ describe('Validation test', () => {
],
},
]
const coreTypes = [
{name: 'array', type: 'type'},
{name: 'object', type: 'type'},
{name: 'block', type: 'type'},
{name: 'reference', type: 'type'},
{name: 'image', type: 'type'},
{name: 'file', type: 'type'},
]

const visitors = {
array: {visit: array},
object: {visit: object},
block: {visit: block},
reference: {visit: reference},
image: {visit: image},
file: {visit: file},
}

const validation = traverseSchema(schemaDef, coreTypes, (typeDef, visitorContext) => {
const visitor = visitors[typeDef.type]
return visitor ? visitor.visit(typeDef, visitorContext) : typeDef
})
const validation = validateSchema(schemaDef)

const validObjectResult = validation.get('validObject')
expect(validObjectResult._problems).toHaveLength(0)
Expand All @@ -180,7 +139,7 @@ describe('Validation test', () => {
const problems = flatten(
invalidObjectResult.fields[0].of[0].of.map((item) => item._problems),
).filter(Boolean)
expect(problems).toHaveLength(6)
expect(problems).toHaveLength(7)
expect(problems[0]).toMatchObject({
severity: 'error',
helpId: 'schema-array-of-type-builtin-type-conflict',
Expand All @@ -202,6 +161,10 @@ describe('Validation test', () => {
helpId: 'schema-array-of-type-global-type-conflict',
})
expect(problems[5]).toMatchObject({
severity: 'warning',
helpId: 'schema-array-of-type-global-type-conflict',
})
expect(problems[6]).toMatchObject({
severity: 'error',
helpId: 'schema-array-of-type-builtin-type-conflict',
})
Expand Down

2 comments on commit b3b365e

@vercel
Copy link

@vercel vercel bot commented on b3b365e Sep 6, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

performance-studio – ./

performance-studio.sanity.build
performance-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on b3b365e Sep 6, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.