import { expect } from '@open-wc/testing'; import { Validator } from '../src/Validator.js'; import { ConfigurationInterface } from '../src/Entities/ConfigurationInterface.js'; describe('Validator', () => { it('does not return any error if the configuration is correct', () => { const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', context: { type: 'course', id: 'course_id', }, height: 1, width: 2, debug: true, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(0); }); it('returns error messages if the dimension parameters are out of bound', () => { const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', context: { type: 'course', id: 'course_id', }, height: 0, width: 0, debug: true, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(2); expect(validator.getValidation().errors).includes( 'The width should be at least 1.' ); expect(validator.getValidation().errors).includes( 'The height should be at least 1.' ); }); it('returns an error message if the height parameter is out of bound', () => { const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', context: { type: 'course', id: 'course_id', }, height: 0, width: 100, debug: true, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(1); expect(validator.getValidation().errors).includes( 'The height should be at least 1.' ); expect(validator.getValidation().errors).to.not.include( 'The width should be at least 1.' ); }); it('returns an error message if the width parameter is out of bound', () => { const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', context: { type: 'course', id: 'course_id', }, height: 100, width: 0, debug: true, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(1); expect(validator.getValidation().errors).includes( 'The width should be at least 1.' ); expect(validator.getValidation().errors).to.not.include( 'The height should be at least 1.' ); }); it('returns an error message if the width parameter is not a number', () => { const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', context: { type: 'course', id: 'course_id', }, height: 100, // @ts-ignore width: 'foo', debug: true, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(1); expect(validator.getValidation().errors).includes( 'The width field should be a number.' ); }); it('returns an error message if the height parameter is not a number', () => { const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', context: { type: 'course', id: 'course_id', }, // @ts-ignore height: 'foo', width: 100, debug: true, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(1); expect(validator.getValidation().errors).includes( 'The height field should be a number.' ); }); it('returns an error message if the token is not passed', () => { // @ts-ignore const configParams: ConfigurationInterface = { host: 'test-host', context: { type: 'course', id: 'course_id', }, height: 100, width: 100, debug: true, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(1); expect(validator.getValidation().errors).includes( 'The token field is required.' ); }); it('returns an error message if the context is not passed', () => { // @ts-ignore const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', height: 100, width: 100, debug: true, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(1); expect(validator.getValidation().errors).includes( 'The context field is required.' ); }); it('returns an error message if the context is not an object', () => { const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', // @ts-ignore context: 'invalid-context', height: 100, width: 100, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(1); expect(validator.getValidation().errors).includes( 'The context field should be an object.' ); }); it('returns an error message if the context is not an valid object (instance of ContextEntityInterface)', () => { const configParams: ConfigurationInterface = { token: 'test-token', host: 'test-host', // @ts-ignore context: {}, height: 100, width: 100, }; const validator: Validator = new Validator(configParams); expect(validator.getValidation().errors.length).to.be.equal(1); expect(validator.getValidation().errors).includes( 'The context field should be valid object.' ); }); });