import { GCMockModule } from '@core/mocks/gc-module.mock'; import { EvaluationType } from '@features/formio/form-logic-evaluator.typing'; import { Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect, spy } from 'chai'; import { FormLogicEvaluatorService } from './form-logic-evaluator.service'; @DescribeAngularService(FormLogicEvaluatorService, { imports: [GCMockModule] }) export class FormLogicEvaluatorServiceSpec implements Spec { mockFuncs (service: FormLogicEvaluatorService) { const calcValue = service.wrapFunction(EvaluationType.CalculatedValue, () => {}); const visibility = service.wrapFunction(EvaluationType.Visibility, () => {}); const validation = service.wrapFunction(EvaluationType.Validation, () => {}); const fns = [calcValue, visibility, validation]; return fns; } @TestCase('should be able to run a basic function') testShouldRunBasicFunction (service: FormLogicEvaluatorService) { const varName = 'someArg'; const fn = 'someArg = "Good stuff!"'; const result = service.evaluateFunction(fn, {}, varName, ''); expect(result).to.equal('Good stuff!'); } @TestCase('should be able to use arguments') testShouldUseArguments (service: FormLogicEvaluatorService) { const varName = 'someArg'; const fn = 'someArg = input'; const expected = 'expected'; const result = service.evaluateFunction(fn, { input: expected }, varName, ''); expect(result).to.equal(expected); } @TestCase('should be able to get filtered functions - init') getFilteredFuncsInit (service: FormLogicEvaluatorService) { const fns = this.mockFuncs(service); const filtered = service.getFilteredFuncs( fns, true, false ); // No validation fired on init const noValidation = filtered.every((fn) => { return fn.type !== EvaluationType.Validation; }); expect(noValidation).to.be.true; } @TestCase('should be able to get filtered functions - read only') getFilteredFuncsReadOnly (service: FormLogicEvaluatorService) { const fns = this.mockFuncs(service); const filtered = service.getFilteredFuncs( fns, false, true ); // Read only - visibility is the only thing run const onlyVisibility = filtered.every((fn) => { return fn.type === EvaluationType.Visibility; }); expect(onlyVisibility).to.be.true; } @TestCase('should be able to get filtered functions - all') getFilteredFuncsAll (service: FormLogicEvaluatorService) { const fns = this.mockFuncs(service); const filtered = service.getFilteredFuncs( fns, false, false ); // Should return all expect(filtered).to.deep.equal(fns); } @TestCase('should be able to execute js function') executeJSFunctions (service: FormLogicEvaluatorService) { const fn = spy(this.mockFuncs(service)[0]); const wrapped = service.wrapFunction(EvaluationType.CalculatedValue, fn); service.executeJSFunctions([wrapped], false, false, {}); expect(fn).to.have.been.called; } @TestCase('should be able to execute js function - fall through') executeJSFunctionsFall (service: FormLogicEvaluatorService) { const { applyResults, numberOfExecutions } = service.executeJSFunctions([], false, false, {}); expect(applyResults).to.be.false; expect(numberOfExecutions).to.be.equal(0); } }