Skip to content

Commit

Permalink
test(common): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minenwerfer committed Oct 16, 2024
1 parent ad923eb commit c4000cc
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/common/tests/checkForEmptiness.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from 'vitest'
import { checkForEmptiness } from '../src/index.js'

test('checkForEmptiness() returns correctly', async () => {
expect(checkForEmptiness({ type: 'string', }, 'name', { name: '', })).toBeTruthy()
expect(checkForEmptiness({ type: 'string', }, 'name', { name: null, })).toBeTruthy()
expect(checkForEmptiness({ type: 'string', }, 'name', { name: undefined, })).toBeTruthy()

expect(checkForEmptiness({ type: 'string', format: 'date-time', isTimestamp: true, }, 'created_at', {})).toBeFalsy()
expect(checkForEmptiness({ type: 'string', readOnly: true, }, 'name', {})).toBeFalsy()

expect(checkForEmptiness({ type: 'string', }, 'name', { name: 'terry', })).toBeFalsy()
expect(checkForEmptiness({ type: 'boolean', }, 'active', {})).toBeTruthy()
expect(checkForEmptiness({ type: 'boolean', }, 'active', { active: false, })).toBeFalsy()
})


36 changes: 36 additions & 0 deletions packages/common/tests/freshItem.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, test } from 'vitest'
import { freshItem } from '../src/index.js'

test('freshItem() returns correctly', async () => {
expect(freshItem({
properties: {
name: {
type: 'string',
},
pet: {
$ref: 'pet',
},
nested: {
type: 'object',
properties: {
boolean: {
type: 'boolean',
},
},
},
array: {
type: 'array',
items: {
type: 'string',
},
},
},
})).toStrictEqual({
nested: {
boolean: false,
},
array: [],
})
})


19 changes: 19 additions & 0 deletions packages/common/tests/getMissingProperties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { JsonSchema } from '@aeriajs/types'
import { expect, test } from 'vitest'
import { getMissingProperties } from '../src/index.js'

test('getMissingProperties() returns correctly', async () => {
const schema: JsonSchema = {
$id: 'person',
properties: {
name: {
type: 'string',
},
},
}

expect(getMissingProperties({}, schema, [])).toHaveLength(0)
expect(getMissingProperties({}, schema, ['name'])).toHaveLength(1)
expect(getMissingProperties({ name: 'terry', }, schema, ['name'])).toHaveLength(0)
})

37 changes: 37 additions & 0 deletions packages/common/tests/isRequired.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { RequiredProperties, JsonSchema } from '@aeriajs/types'
import { expect, test } from 'vitest'
import { isRequired } from '../src/index.js'

test('isRequired() returns correctly with array', async () => {
const requiredArray: RequiredProperties<JsonSchema> = [
'name',
]

expect(isRequired('name', requiredArray, {})).toBeTruthy()
expect(isRequired('job', requiredArray, {})).toBeFalsy()
})

test('isRequired() returns correctly with object', async () => {
const requiredKeyValue: RequiredProperties<JsonSchema> = {
name: true,
job: false,
}

expect(isRequired('name', requiredKeyValue, {})).toBeTruthy()
expect(isRequired('job', requiredKeyValue, {})).toBeFalsy()
expect(isRequired('age', requiredKeyValue, {})).toBeFalsy()
})

test('isRequired() returns correctly with condition', async () => {
const requiredCondition: RequiredProperties<JsonSchema> = {
responsible: {
operator: 'lt',
term1: 'age',
term2: 18,
},
}

expect(isRequired('responsible', requiredCondition, { age: 17, })).toBeTruthy()
expect(isRequired('responsible', requiredCondition, { age: 18, })).toBeFalsy()
})

0 comments on commit c4000cc

Please sign in to comment.