Skip to content

Commit

Permalink
Unit test isItemValid
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 26, 2024
1 parent 7e33991 commit f9cdc89
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/dataviews/src/test/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Internal dependencies
*/
import { isItemValid } from '../validation';
import type { Field } from '../types';

describe( 'validation', () => {
it( 'fields not visible in form are not validated', () => {
const item = { id: 1, valid_order: 2, invalid_order: 'd' };
const fields: Field< {} >[] = [
{
id: 'valid_order',
type: 'integer',
},
{
id: 'invalid_order',
type: 'integer',
},
];
const form = { visibleFields: [ 'valid_order' ] };
const result = isItemValid( item, fields, form );
expect( result ).toBe( true );
} );

it( 'integer field is valid if value is integer', () => {
const item = { id: 1, order: 2, title: 'hi' };
const fields: Field< {} >[] = [
{
type: 'integer',
id: 'order',
},
];
const form = { visibleFields: [ 'order' ] };
const result = isItemValid( item, fields, form );
expect( result ).toBe( true );
} );

it( 'integer field is invalid if value is not integer', () => {
const item = { id: 1, order: 'd' };
const fields: Field< {} >[] = [
{
id: 'order',
type: 'integer',
},
];
const form = { visibleFields: [ 'order' ] };
const result = isItemValid( item, fields, form );
expect( result ).toBe( false );
} );

it( 'integer field is invalid if value is empty', () => {
const item = { id: 1, order: '' };
const fields: Field< {} >[] = [
{
id: 'order',
type: 'integer',
},
];
const form = { visibleFields: [ 'order' ] };
const result = isItemValid( item, fields, form );
expect( result ).toBe( false );
} );
} );

0 comments on commit f9cdc89

Please sign in to comment.