Skip to content

Commit

Permalink
Removed chai from tests (was causing trouble and is not really needed)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei15193 committed Mar 18, 2024
1 parent 27e73b8 commit 99691ed
Show file tree
Hide file tree
Showing 14 changed files with 662 additions and 833 deletions.
520 changes: 182 additions & 338 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,17 @@
},
"devDependencies": {
"@testing-library/react": "^14.2.1",
"@types/chai": "^4.3.12",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.20",
"@types/react": "^18.2.59",
"chai": "^5.1.0",
"@types/node": "^20.11.29",
"@types/react": "^18.2.67",
"global-jsdom": "^24.0.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"react-dom": "^18.2.0",
"ts-jest": "^29.1.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"typescript": "^5.4.2",
"webpack": "^5.90.3",
"webpack-cli": "^5.1.4"
},
Expand Down
15 changes: 7 additions & 8 deletions tests/components/input-tests.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react';
import { expect } from 'chai';
import { Input } from '../../src/components/input';
import { FormFieldViewModel } from '../../src/form-field-view-model';

Expand All @@ -9,7 +8,7 @@ describe('watch-event/useViewModelType', (): void => {
const field = new FormFieldViewModel('test-field', 0);
const { getByDisplayValue } = render(<Input field={field} defaultValue="test" />);

expect(getByDisplayValue('test')).is.not.undefined;
expect(getByDisplayValue('test')).not.toBe(undefined);
});

it('focusing input focuses field', (): void => {
Expand All @@ -18,7 +17,7 @@ describe('watch-event/useViewModelType', (): void => {

getByDisplayValue('test').focus();

expect(field.isFocused).to.be.true;
expect(field.isFocused).toBe(true);
});

it('focusing field focuses input', (): void => {
Expand All @@ -27,18 +26,18 @@ describe('watch-event/useViewModelType', (): void => {

field.isFocused = true;

expect(document.activeElement).is.equal(getByDisplayValue('test'));
expect(document.activeElement).toBe(getByDisplayValue('test'));
});

it('bluring input de-focuses field', (): void => {
const field = new FormFieldViewModel('test-field', 0);
const { getByDisplayValue } = render(<Input field={field} defaultValue="test" />);
getByDisplayValue('test').focus();
expect(field.isFocused).to.be.true;
expect(field.isFocused).toBe(true);

getByDisplayValue('test').blur();

expect(field.isFocused).to.be.false;
expect(field.isFocused).toBe(false);
});

it('de-focusing field blurs input', (): void => {
Expand All @@ -49,7 +48,7 @@ describe('watch-event/useViewModelType', (): void => {
field.isFocused = false;


expect(document.activeElement).is.not.equal(getByDisplayValue('test'));
expect(document.activeElement).not.toStrictEqual(getByDisplayValue('test'));
});

it('passing focused field focuses input', (): void => {
Expand All @@ -58,6 +57,6 @@ describe('watch-event/useViewModelType', (): void => {

const { getByDisplayValue } = render(<Input field={field} defaultValue="test" />);

expect(document.activeElement).is.equal(getByDisplayValue('test'));
expect(document.activeElement).toBe(getByDisplayValue('test'));
});
});
17 changes: 8 additions & 9 deletions tests/events-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IEventHandler } from '../src/events';
import { expect } from 'chai';
import { IEventHandler } from '../src/events';
import { EventDispatcher } from '../src/events';

describe('events/EventDispatcher', (): void => {
Expand All @@ -17,13 +16,13 @@ describe('events/EventDispatcher', (): void => {
eventDispatcher.subscribe({
handle(actualSubject, actualArgs) {
invocationCount++;
expect(actualSubject).is.equal(subject);
expect(actualArgs).is.equal(args);
expect(actualSubject).toStrictEqual(subject);
expect(actualArgs).toStrictEqual(args);
}
})
eventDispatcher.dispatch(subject, args);

expect(invocationCount).is.equal(1);
expect(invocationCount).toStrictEqual(1);
});

it('dispatching event to subscriber that unsubscribes next one no longer notifies it', (): void => {
Expand All @@ -46,8 +45,8 @@ describe('events/EventDispatcher', (): void => {

eventDispatcher.dispatch(null);

expect(firstInvocationCount).is.equal(1);
expect(secondInvocationCount).is.equal(0);
expect(firstInvocationCount).toBe(1);
expect(secondInvocationCount).toBe(0);
});

it('dispatching event to handler subscribed twice notifies it twice', (): void => {
Expand All @@ -63,7 +62,7 @@ describe('events/EventDispatcher', (): void => {

eventDispatcher.dispatch(null);

expect(invocationCount).is.equal(2);
expect(invocationCount).toBe(2);
});

it('dispatching event to handler subscribed twice then unsubscribed once notifies it once', (): void => {
Expand All @@ -80,6 +79,6 @@ describe('events/EventDispatcher', (): void => {

eventDispatcher.dispatch(null);

expect(invocationCount).is.equal(1);
expect(invocationCount).toBe(1);
});
});
100 changes: 49 additions & 51 deletions tests/form-field-collection-view-model-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FormFieldViewModel } from '../src/form-field-view-model';
import { expect } from 'chai';
import { FormFieldCollectionViewModel } from '../src/form-field-collection-view-model';

describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): void => {
Expand All @@ -23,17 +22,17 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
formFieldCollection.propertiesChanged.subscribe({
handle(subject, changedProperties) {
invocationCount++;
expect(subject).is.equal(formFieldCollection);
expect(changedProperties).is.deep.equal(['error', 'isValid', 'isInvalid']);
expect(formFieldCollection.error).is.equal('');
expect(formFieldCollection.isValid).is.equal(false);
expect(formFieldCollection.isInvalid).is.equal(true);
expect(subject).toStrictEqual(formFieldCollection);
expect(changedProperties).toEqual(['error', 'isValid', 'isInvalid']);
expect(formFieldCollection.error).toBe('');
expect(formFieldCollection.isValid).toBe(false);
expect(formFieldCollection.isInvalid).toBe(true);
}
});

formFieldCollection.error = '';

expect(invocationCount).is.equal(1);
expect(invocationCount).toBe(1);
});

it('setting same error does not notify subscribers', (): void => {
Expand All @@ -48,7 +47,7 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo

formFieldCollection.error = '';

expect(invocationCount).is.equal(0);
expect(invocationCount).toBe(0);
});

it('setting error back to undefined notifies subscribers and updates related flags', (): void => {
Expand All @@ -58,17 +57,17 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
formFieldCollection.propertiesChanged.subscribe({
handle(subject, changedProperties) {
invocationCount++;
expect(subject).is.equal(formFieldCollection);
expect(changedProperties).is.deep.equal(['error', 'isValid', 'isInvalid']);
expect(formFieldCollection.error).is.equal(undefined);
expect(formFieldCollection.isValid).is.equal(true);
expect(formFieldCollection.isInvalid).is.equal(false);
expect(subject).toStrictEqual(formFieldCollection);
expect(changedProperties).toEqual(['error', 'isValid', 'isInvalid']);
expect(formFieldCollection.error).toBe(undefined);
expect(formFieldCollection.isValid).toBe(true);
expect(formFieldCollection.isInvalid).toBe(false);
}
});

formFieldCollection.error = undefined;

expect(invocationCount).is.equal(1);
expect(invocationCount).toBe(1);
});

it('registering a field updates related flags', (): void => {
Expand All @@ -77,17 +76,17 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
formFieldCollection.propertiesChanged.subscribe({
handle(subject, changedProperties) {
invocationCount++;
expect(subject).is.equal(formFieldCollection);
expect(changedProperties).is.deep.equal(['isValid', 'isInvalid']);
expect(formFieldCollection.error).is.equal(undefined);
expect(formFieldCollection.isValid).is.equal(true);
expect(formFieldCollection.isInvalid).is.equal(false);
expect(subject).toStrictEqual(formFieldCollection);
expect(changedProperties).toEqual(['isValid', 'isInvalid']);
expect(formFieldCollection.error).toBe(undefined);
expect(formFieldCollection.isValid).toBe(true);
expect(formFieldCollection.isInvalid).toBe(false);
}
});

formFieldCollection.addField('', '');

expect(invocationCount).is.equal(1);
expect(invocationCount).toBe(1);
});

it('registering and invalidating a field updates related flags', (): void => {
Expand All @@ -97,17 +96,17 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
formFieldCollection.propertiesChanged.subscribe({
handle(subject, changedProperties) {
invocationCount++;
expect(subject).is.equal(formFieldCollection);
expect(changedProperties).is.deep.equal(['isValid', 'isInvalid']);
expect(formFieldCollection.error).is.equal(undefined);
expect(formFieldCollection.isValid).is.equal(false);
expect(formFieldCollection.isInvalid).is.equal(true);
expect(subject).toStrictEqual(formFieldCollection);
expect(changedProperties).toEqual(['isValid', 'isInvalid']);
expect(formFieldCollection.error).toBe(undefined);
expect(formFieldCollection.isValid).toBe(false);
expect(formFieldCollection.isInvalid).toBe(true);
}
});

field.error = '';

expect(invocationCount).is.equal(1);
expect(invocationCount).toBe(1);
});

it('registering and validating a field updates related flags', (): void => {
Expand All @@ -118,17 +117,17 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
formFieldCollection.propertiesChanged.subscribe({
handle(subject, changedProperties) {
invocationCount++;
expect(subject).is.equal(formFieldCollection);
expect(changedProperties).is.deep.equal(['isValid', 'isInvalid']);
expect(formFieldCollection.error).is.equal(undefined);
expect(formFieldCollection.isValid).is.equal(true);
expect(formFieldCollection.isInvalid).is.equal(false);
expect(subject).toStrictEqual(formFieldCollection);
expect(changedProperties).toEqual(['isValid', 'isInvalid']);
expect(formFieldCollection.error).toBe(undefined);
expect(formFieldCollection.isValid).toBe(true);
expect(formFieldCollection.isInvalid).toBe(false);
}
});

field.error = undefined;

expect(invocationCount).is.equal(1);
expect(invocationCount).toBe(1);
});

it('unregistering a field updates related flags', (): void => {
Expand All @@ -138,17 +137,17 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
formFieldCollection.propertiesChanged.subscribe({
handle(subject, changedProperties) {
invocationCount++;
expect(subject).is.equal(formFieldCollection);
expect(changedProperties).is.deep.equal(['isValid', 'isInvalid']);
expect(formFieldCollection.error).is.equal(undefined);
expect(formFieldCollection.isValid).is.equal(true);
expect(formFieldCollection.isInvalid).is.equal(false);
expect(subject).toStrictEqual(formFieldCollection);
expect(changedProperties).toEqual(['isValid', 'isInvalid']);
expect(formFieldCollection.error).toBe(undefined);
expect(formFieldCollection.isValid).toBe(true);
expect(formFieldCollection.isInvalid).toBe(false);
}
});

formFieldCollection.unregisterField(field);

expect(invocationCount).is.equal(1);
expect(invocationCount).toBe(1);
});

it('unregistering an invalid field updates related flags', (): void => {
Expand All @@ -159,17 +158,17 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
formFieldCollection.propertiesChanged.subscribe({
handle(subject, changedProperties) {
invocationCount++;
expect(subject).is.equal(formFieldCollection);
expect(changedProperties).is.deep.equal(['isValid', 'isInvalid']);
expect(formFieldCollection.error).is.equal(undefined);
expect(formFieldCollection.isValid).is.equal(true);
expect(formFieldCollection.isInvalid).is.equal(false);
expect(subject).toStrictEqual(formFieldCollection);
expect(changedProperties).toEqual(['isValid', 'isInvalid']);
expect(formFieldCollection.error).toBe(undefined);
expect(formFieldCollection.isValid).toBe(true);
expect(formFieldCollection.isInvalid).toBe(false);
}
});

formFieldCollection.unregisterField(field);

expect(invocationCount).is.equal(1);
expect(invocationCount).toBe(1);
});

it('registering a field makes it available through the fields property', (): void => {
Expand All @@ -179,7 +178,7 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
const field2 = formFieldCollection.addField('', '');
const field3 = formFieldCollection.addField('', '');

expect(formFieldCollection.fields.toArray()).is.deep.equal([field1, field2, field3]);
expect(formFieldCollection.fields.toArray()).toEqual([field1, field2, field3]);
});

it('unregistering a field no longer makes it available through the fields property', (): void => {
Expand All @@ -190,7 +189,7 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo

formFieldCollection.unregisterField(field2);

expect(formFieldCollection.fields.toArray()).is.deep.equal([field1, field3]);
expect(formFieldCollection.fields.toArray()).toEqual([field1, field3]);
});

it('registering a field twice and unregistering it once makes it available through the fields property once', (): void => {
Expand All @@ -200,7 +199,7 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo

formFieldCollection.unregisterField(field);

expect(formFieldCollection.fields.toArray()).is.deep.equal([field]);
expect(formFieldCollection.fields.toArray()).toEqual([field]);
});

it('creating a dynamic form registers all fields', (): void => {
Expand All @@ -209,9 +208,8 @@ describe('form-field-collection-view-model/FormFieldCollectionViewModel', (): vo
field2: new FormFieldViewModel('field2', null)
});

expect(formFieldCollection.fields.toArray())
.does.include(formFieldCollection.field1)
.and.does.include(formFieldCollection.field2)
.and.property('length').is.equal(2);
expect(formFieldCollection.fields.toArray()).toContain(formFieldCollection.field1);
expect(formFieldCollection.fields.toArray()).toContain(formFieldCollection.field2);
expect(formFieldCollection.fields.toArray().length).toBe(2);
});
});
Loading

0 comments on commit 99691ed

Please sign in to comment.