Skip to content

Commit

Permalink
fix: improve error messages in jasmine
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-stepanenko committed Oct 20, 2021
1 parent cd38cad commit 00953c7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

`ngx-cva-test-suite` provides extensive set of test cases, ensuring your custom controls behave as intended.

It provides various configurations, that allows even with the most non-standard components to be properly tested.
It provides various configurations, that allows even the most non-standard components to be properly tested.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion libs/ngx-cva-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Standardise your custom UI form components with ControlValueAccessor Test Suite",
"author": "Dmitriy Stepanenko",
"license": "MIT",
"version": "1.0.3",
"version": "1.0.4",
"repository": {
"type": "git",
"url": "https://github.com/dmitry-stepanenko/ngx-cva-test-suite"
Expand Down
24 changes: 12 additions & 12 deletions libs/ngx-cva-test-suite/src/lib/ngx-cva-test-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export function runValueAccessorTests<T extends CVAComponentType, H = T>(config:
});

beforeEach(() => {
onChangeSpy = testRunnerResolver.createSpy();
onTouchedSpy = testRunnerResolver.createSpy();
onChangeSpy = testRunnerResolver.createSpy('onChangeSpy');
onTouchedSpy = testRunnerResolver.createSpy('onTouchedSpy');

registerOnChangeSpy = testRunnerResolver.spyOn(componentInstance, 'registerOnChange');
registerOnTouchedSpy = testRunnerResolver.spyOn(componentInstance, 'registerOnTouched');
Expand All @@ -96,9 +96,9 @@ export function runValueAccessorTests<T extends CVAComponentType, H = T>(config:
fixture.detectChanges();
}

function expectComponentValueToBe(expected: any): void {
function expectComponentValueToEqual(expected: any): void {
if (typeof config.getComponentValue === 'function') {
expect(config.getComponentValue(fixture)).toBe(expected);
expect(config.getComponentValue(fixture)).toEqual(expected);
}
}

Expand All @@ -121,7 +121,7 @@ export function runValueAccessorTests<T extends CVAComponentType, H = T>(config:
expect(onChangeSpy).toHaveBeenCalledTimes(0);
expect(onTouchedSpy).toHaveBeenCalledTimes(0);
expect(setDisabledStateSpy).toHaveBeenCalledTimes(0);
expectComponentValueToBe(getValuesFn()[0]);
expectComponentValueToEqual(getValuesFn()[0]);
});

it('value set externally', fakeAsync(() => {
Expand All @@ -134,14 +134,14 @@ export function runValueAccessorTests<T extends CVAComponentType, H = T>(config:
expect(onTouchedSpy).toHaveBeenCalledTimes(0);
componentInstance.writeValue(getValuesFn()[1]);
tick(config.customDelay ?? 100);
expectComponentValueToBe(getValuesFn()[1]);
expectComponentValueToEqual(getValuesFn()[1]);
expect(writeValueSpy).toHaveBeenCalledTimes(2);
testRunnerResolver.expectToHaveBeenNthCalledWith(writeValueSpy, 2, [getValuesFn()[1]]);
expect(onChangeSpy).toHaveBeenCalledTimes(0);
expect(onTouchedSpy).toHaveBeenCalledTimes(0);
componentInstance.writeValue(getValuesFn()[2]);
tick(config.customDelay ?? 100);
expectComponentValueToBe(getValuesFn()[2]);
expectComponentValueToEqual(getValuesFn()[2]);
expect(writeValueSpy).toHaveBeenCalledTimes(3);
testRunnerResolver.expectToHaveBeenNthCalledWith(writeValueSpy, 3, [getValuesFn()[2]]);
expect(onChangeSpy).toHaveBeenCalledTimes(0);
Expand All @@ -153,14 +153,14 @@ export function runValueAccessorTests<T extends CVAComponentType, H = T>(config:
expect(writeValueSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledTimes(0);
expect(onTouchedSpy).toHaveBeenCalledTimes(0);
expectComponentValueToBe(getValuesFn()[0]);
expectComponentValueToEqual(getValuesFn()[0]);
componentInstance.writeValue(null);
tick(config.customDelay ?? 100);
expect(writeValueSpy).toHaveBeenCalledTimes(2);
testRunnerResolver.expectToHaveBeenNthCalledWith(writeValueSpy, 2, [null]);
expect(onChangeSpy).toHaveBeenCalledTimes(0);
expect(onTouchedSpy).toHaveBeenCalledTimes(0);
expectComponentValueToBe(config.resetCustomValue ? config.resetCustomValue.value : null);
expectComponentValueToEqual(config.resetCustomValue ? config.resetCustomValue.value : null);
}));

if (!config.disabledStateNotSupported) {
Expand All @@ -170,7 +170,7 @@ export function runValueAccessorTests<T extends CVAComponentType, H = T>(config:
componentInstance.setDisabledState(true);
tick(config.customDelay ?? 100);
componentInstance.writeValue(getValuesFn()[1]);
expectComponentValueToBe(getValuesFn()[1]);
expectComponentValueToEqual(getValuesFn()[1]);
testRunnerResolver.expectToHaveBeenNthCalledWith(writeValueSpy, 2, [getValuesFn()[1]]);
expect(setDisabledStateSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledTimes(0);
Expand All @@ -180,7 +180,7 @@ export function runValueAccessorTests<T extends CVAComponentType, H = T>(config:
tick(config.customDelay ?? 100);
componentInstance.writeValue(getValuesFn()[2]);
tick(config.customDelay ?? 100);
expectComponentValueToBe(getValuesFn()[2]);
expectComponentValueToEqual(getValuesFn()[2]);
testRunnerResolver.expectToHaveBeenNthCalledWith(writeValueSpy, 3, [getValuesFn()[2]]);
expect(setDisabledStateSpy).toHaveBeenCalledTimes(2);
expect(onChangeSpy).toHaveBeenCalledTimes(0);
Expand All @@ -199,7 +199,7 @@ export function runValueAccessorTests<T extends CVAComponentType, H = T>(config:
expect(onTouchedSpy).toHaveBeenCalledTimes(0);
config.internalValueChangeSetter!(fixture, getValuesFn()[1]);
tick(config.customDelay ?? 100);
expectComponentValueToBe(getValuesFn()[1]);
expectComponentValueToEqual(getValuesFn()[1]);
testRunnerResolver.expectToHaveBeenNthCalledWith(onChangeSpy, 1, [getValuesFn()[1]]);
expect(onTouchedSpy).toHaveBeenCalledTimes(config.supportsOnBlur ? 0 : 1);
}));
Expand Down
8 changes: 6 additions & 2 deletions libs/ngx-cva-test-suite/src/lib/utils/test-runner-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ export class TestRunnerResolver {
this.testRunnerType = forceType ?? this.getTestRunnerType();
}

createSpy(): CompatibleSpy {
/**
* @param name name of the spy. Will be used only in jasmine
*/
createSpy(name: string): CompatibleSpy {
switch (this.testRunnerType) {
case TestRunnerType.Jasmine:
return jasmine.createSpy();
return jasmine.createSpy(name);

case TestRunnerType.Jest:
return jest.fn();
Expand Down Expand Up @@ -44,6 +47,7 @@ export class TestRunnerResolver {
(<any>expect(spyFn)).toHaveBeenNthCalledWith(nthCall, ...params);
} else {
const nthCallArgs = spyFn.calls.argsFor(nthCall - 1);
expect(spyFn).toHaveBeenCalledTimes(nthCall);
expect(nthCallArgs).toEqual(params);
}
}
Expand Down

0 comments on commit 00953c7

Please sign in to comment.