From 1e3391a99357703de4b128ec2500528938fe1f59 Mon Sep 17 00:00:00 2001 From: Xymorot Date: Sat, 12 Oct 2019 03:52:08 +0200 Subject: [PATCH] test: implement tests for renderer util service --- tests/renderer/services/utils.spec.ts | 97 +++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/renderer/services/utils.spec.ts diff --git a/tests/renderer/services/utils.spec.ts b/tests/renderer/services/utils.spec.ts new file mode 100644 index 0000000..f6eb9aa --- /dev/null +++ b/tests/renderer/services/utils.spec.ts @@ -0,0 +1,97 @@ +import { expect } from 'chai'; +import fc from 'fast-check'; +import { describe, it } from 'mocha'; +import { c, s, t } from '../../../src/renderer/services/utils'; + +describe('Frontend Utils', function() { + this.timeout(1000); + + it("does 'translate' text", () => { + expect(t('translate this')).to.equal('translate this'); + }); + + describe('Class Util', () => { + beforeEach(function() { + const numeric = fc.integer(0x30, 0x39); + const alphaCapital = fc.integer(0x41, 0x5a); + const alphaSmall = fc.integer(0x61, 0x7a); + const alpha = fc.oneof(alphaCapital, alphaSmall).map(String.fromCharCode); + const alphaNumeric = fc.oneof(numeric, alphaCapital, alphaSmall).map(String.fromCharCode); + + this.className = fc + .stringOf(fc.oneof(alpha, alphaNumeric, fc.constantFrom('-', '_'))) + .filter((name: string) => !!name.charAt(0).match(/[a-zA-Z]/)); + this.anything = fc + .anything({ + maxDepth: 1, + maxKeys: 2, + }) + .filter((thing) => typeof thing !== 'string'); + }); + + it('compiles classes out of an array', function() { + const classArrayArbitrary = fc.array(fc.oneof(this.className, this.anything)); + + fc.assert( + fc.property(classArrayArbitrary, (classArray) => { + const expectedClasses = classArray.filter((potentialClass) => typeof potentialClass === 'string'); + const compiled = c(classArray); + + expect(compiled).to.be.a('string', 'class util does not return a string'); + expectedClasses.forEach((expectedClassName) => { + expect(compiled).to.contain(expectedClassName, 'class util does not add all classes'); + }); + }) + ); + }); + + it('compiles classes out of an object', function() { + const classObjectArbitrary = fc.object({ + maxDepth: 0, + key: this.className, + values: [fc.boolean()], + }); + + fc.assert( + fc.property(classObjectArbitrary, (classObject) => { + const expectedClasses = Object.keys(classObject).filter((key) => classObject[key]); + const compiled = c(classObject); + + expect(compiled).to.be.a('string', 'class util does not return a string'); + expectedClasses.forEach((className) => { + expect(compiled).to.contain(className, 'class util does not add all classes'); + }); + }) + ); + }); + }); + + describe('Style Util', () => { + it('compiles an object into an html style property', () => { + const styleObjectArbitrary = fc.object({ + maxDepth: 0, + key: fc.string(1, 10).filter((str) => !str.match(/\s/)), + values: [fc.string(), fc.integer(), fc.boolean()], + }); + + fc.assert( + fc.property(styleObjectArbitrary, (styleObject) => { + const expectedString = Object.keys(styleObject) + .filter((key) => { + const value = styleObject[key]; + if (typeof value === 'string') { + return !!value; + } + return typeof value === 'number'; + }) + .map((key) => { + return `${key}:${styleObject[key]}`; + }) + .join(';'); + + expect(s(styleObject)).to.equal(expectedString); + }) + ); + }); + }); +});