Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
All dependecies are up to date + jest.mocked instead of ts-jest mocke…
Browse files Browse the repository at this point in the history
…d due to jestjs/jest#12089
  • Loading branch information
boonya committed Dec 18, 2021
1 parent f7851f2 commit 837b6fe
Show file tree
Hide file tree
Showing 17 changed files with 994 additions and 991 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.12
16
1,830 changes: 918 additions & 912 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@
},
"devDependencies": {
"@testing-library/react-hooks": "^7.0.2",
"@types/jest": "^27.0.2",
"@types/lodash": "^4.14.176",
"@types/react": "^17.0.31",
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/parser": "^5.1.0",
"@types/jest": "^27.0.3",
"@types/lodash": "^4.14.178",
"@types/react": "^17.0.37",
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
"eslint": "^7.32.0",
"eslint-plugin-react-hooks": "^4.2.0",
"jest": "^27.3.1",
"eslint-plugin-react-hooks": "^4.3.0",
"jest": "^27.4.5",
"react-test-renderer": "^17.0.2",
"ts-jest": "^27.0.7",
"typescript": "^4.4.4"
"ts-jest": "^27.1.2",
"typescript": "^4.5.4"
}
}
3 changes: 1 addition & 2 deletions src/processor/processorCreator.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {ValidationRuleSet, FormPayload, FieldState} from '../types';
import processorCreator from './processorCreator';
import calculateValidity from './validityCalculator';
import {mocked} from 'ts-jest/utils';

jest.mock('./validityCalculator');

Expand All @@ -13,7 +12,7 @@ const validity: FieldState = {
};

beforeEach(() => {
mocked(calculateValidity).mockName('calculateValidity').mockResolvedValue(validity);
jest.mocked(calculateValidity).mockName('calculateValidity').mockResolvedValue(validity);
});

describe('Creates a processor function which retrieves a field rules and calculate validity of its payload.', () => {
Expand Down
29 changes: 14 additions & 15 deletions src/processor/validityCalculator.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import calculateValidity from './validityCalculator';
import validityReducerCreator from './validityReducerCreator';
import {mocked} from 'ts-jest/utils';
import {FormPayload, VALIDATORS} from '../types';
import {extractFieldValue} from '../helpers';

Expand All @@ -13,11 +12,11 @@ const validityReducer = jest.fn().mockName('validityReducer');

beforeEach(() => {
validityReducer.mockResolvedValue(null);
mocked(validityReducerCreator).mockName('validityReducerCreator').mockReturnValue(validityReducer);
jest.mocked(validityReducerCreator).mockName('validityReducerCreator').mockReturnValue(validityReducer);
});

test('If no ruleset and no value (value=undefined) the function returns default field validity state.', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(undefined);
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(undefined);

const result = await calculateValidity([], payload, FIELD._1, 0);

Expand Down Expand Up @@ -54,42 +53,42 @@ describe('If there is a value but no ruleset the function returns default field
}

test('If value is 0', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(0);
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(0);
await executeTest();
});

test('If value is "0"', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue('0');
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue('0');
await executeTest();
});

test('If value is null', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(null);
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(null);
await executeTest();
});

test('If value is ""', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue('');
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue('');
await executeTest();
});

test('If value is " "', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(' ');
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(' ');
await executeTest();
});

test('If value is "\\n"', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue('\n');
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue('\n');
await executeTest();
});

test('If value is an empty array', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue([]);
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue([]);
await executeTest();
});

test('If value is an empty object', async () => {
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue({});
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue({});
await executeTest();
});
});
Expand All @@ -98,7 +97,7 @@ test('If there is a single rule specified but a value and index are undefined.',
const RULESET = [{validator: VALIDATORS.required}];
const VALUE = undefined;
const INDEX = 0;
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(VALUE);
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(VALUE);

const result = await calculateValidity(RULESET, payload, FIELD._1, INDEX);

Expand All @@ -122,7 +121,7 @@ test('If there is a single rule specified + a value and index are defined.', asy
const RULESET = [{validator: VALIDATORS.required}];
const VALUE = 'a value';
const INDEX = 2;
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(VALUE);
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(VALUE);

const result = await calculateValidity(RULESET, payload, FIELD._1, INDEX);

Expand All @@ -146,7 +145,7 @@ test('If there is a several rules specified + a value and index are defined.', a
const RULESET = [{validator: VALIDATORS.required}, {validator: VALIDATORS.min}, {validator: VALIDATORS.email}];
const VALUE = 'a value';
const INDEX = 2;
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(VALUE);
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(VALUE);

const result = await calculateValidity(RULESET, payload, FIELD._1, INDEX);

Expand Down Expand Up @@ -179,7 +178,7 @@ test('If validityReducer returns new state.', async () => {
error: true,
message: 'an error',
};
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(VALUE);
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(VALUE);
validityReducer.mockResolvedValue(NEW_STATE);

const result = await calculateValidity(RULESET, payload, FIELD._1, INDEX);
Expand Down
16 changes: 8 additions & 8 deletions src/processor/validityReducerCreator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FieldRule, FormPayload, FieldState, VALIDATORS } from '../types';
import validateValue from '../validators';
import { extractFieldValue } from '../helpers';
import validityReducerCreator from './validityReducerCreator';
import { mocked } from 'ts-jest/utils';


jest.mock('../validators');
jest.mock('../helpers');
Expand All @@ -13,8 +13,8 @@ const FIELDS = {
};

beforeEach(() => {
mocked(validateValue).mockName('validateValue').mockResolvedValue({ error: false, message: 'success' });
mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(undefined);
jest.mocked(validateValue).mockName('validateValue').mockResolvedValue({ error: false, message: 'success' });
jest.mocked(extractFieldValue).mockName('extractFieldValue').mockReturnValue(undefined);
});

const INDEX = 0;
Expand All @@ -27,7 +27,7 @@ describe('Check condition if passed', () => {
test('Returns null if the condition is not met. Condition selector receives no args.', async () => {
const conditionSelector = jest.fn(() => false).mockName('conditionSelector');
const FIELD_RULE: FieldRule = { validator: VALIDATORS.required, condition: [conditionSelector] };
mocked(validateValue).mockName('validateValue').mockResolvedValue({ error: true, message: 'fail' });
jest.mocked(validateValue).mockName('validateValue').mockResolvedValue({ error: true, message: 'fail' });

const reducer = validityReducerCreator(PAYLOAD, FIELD, VALUE, INDEX);
const result = await reducer(Promise.resolve(null), FIELD_RULE);
Expand All @@ -44,10 +44,10 @@ describe('Check condition if passed', () => {
const conditionSelector = jest.fn(() => false).mockName('conditionSelector');
const FIELD_NAMES = [FIELDS._1, FIELDS._1];
const FIELD_RULE: FieldRule = { validator: VALIDATORS.required, condition: [conditionSelector, ...FIELD_NAMES] };
mocked(extractFieldValue).mockName('extractFieldValue')
jest.mocked(extractFieldValue).mockName('extractFieldValue')
.mockReturnValueOnce(`${FIELDS._1} value`)
.mockReturnValueOnce(`${FIELDS._2} value`);
mocked(validateValue).mockName('validateValue').mockResolvedValue({ error: true, message: 'fail' });
jest.mocked(validateValue).mockName('validateValue').mockResolvedValue({ error: true, message: 'fail' });

const reducer = validityReducerCreator(PAYLOAD, FIELD, VALUE, INDEX);
const result = await reducer(Promise.resolve(null), FIELD_RULE);
Expand All @@ -66,7 +66,7 @@ describe('Check condition if passed', () => {
const conditionSelector = jest.fn(() => true).mockName('conditionSelector');
const FIELD_NAMES = [FIELDS._1, FIELDS._1];
const FIELD_RULE: FieldRule = { validator: VALIDATORS.required, condition: [conditionSelector, ...FIELD_NAMES] };
mocked(extractFieldValue).mockName('extractFieldValue')
jest.mocked(extractFieldValue).mockName('extractFieldValue')
.mockReturnValueOnce(`${FIELDS._1} value`)
.mockReturnValueOnce(`${FIELDS._2} value`);

Expand Down Expand Up @@ -106,7 +106,7 @@ describe('Returns FieldState object if validation produces any error.', () => {

async function executeTest(value: unknown) {
const FIELD_RULE: FieldRule = { validator: VALIDATORS.required };
mocked(validateValue).mockName('validateValue').mockResolvedValue({ error: true, message: 'fail' });
jest.mocked(validateValue).mockName('validateValue').mockResolvedValue({ error: true, message: 'fail' });

const reducer = validityReducerCreator(PAYLOAD, FIELD, value, INDEX);
const result = await reducer(Promise.resolve(null), FIELD_RULE);
Expand Down
15 changes: 7 additions & 8 deletions src/useValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import {
processFieldValidity,
validateRuleSet,
} from './helpers';
import { mocked } from 'ts-jest/utils';
import { renderHook, act } from '@testing-library/react-hooks';

jest.mock('./processor');
jest.mock('./helpers');

const validRuleSet: ValidationRuleSet = [{
field: 'fieldname',
field: 'field_name',
rules: [{ validator: VALIDATORS.required }],
}];

Expand All @@ -36,11 +35,11 @@ const newValidity = new Validity([{
const processor = jest.fn().mockName('processor');

beforeEach(() => {
mocked(createProcessor).mockName('createProcessor').mockReturnValue(processor);
mocked(createDefaultValidity).mockName('createDefaultValidity').mockReturnValue(defaultValidity);
mocked(processFormValidity).mockName('processFormValidity').mockResolvedValue(newValidity);
mocked(processFieldValidity).mockName('processFieldValidity').mockResolvedValue(newValidity);
mocked(validateRuleSet).mockName('validateRuleSet').mockReturnValue();
jest.mocked(createProcessor).mockName('createProcessor').mockReturnValue(processor);
jest.mocked(createDefaultValidity).mockName('createDefaultValidity').mockReturnValue(defaultValidity);
jest.mocked(processFormValidity).mockName('processFormValidity').mockResolvedValue(newValidity);
jest.mocked(processFieldValidity).mockName('processFieldValidity').mockResolvedValue(newValidity);
jest.mocked(validateRuleSet).mockName('validateRuleSet').mockReturnValue();
});

function runHook() {
Expand All @@ -51,7 +50,7 @@ function runHook() {

describe('Hook throws an error', () => {
test('no ruleset', () => {
mocked(validateRuleSet).mockName('validateRuleSet').mockImplementation(() => {
jest.mocked(validateRuleSet).mockName('validateRuleSet').mockImplementation(() => {
throw new Error('RuleSet checker throws an error');
});
expect(() => useValidation([])).toThrow(new Error('RuleSet checker throws an error'));
Expand Down
6 changes: 3 additions & 3 deletions src/validators/email.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import validateValue from './email';
import { createValidationMessage, createValidatorResult } from '../helpers';
import { VALIDATION_MESSAGES } from '../types';
import { mocked } from 'ts-jest/utils';


jest.mock('../helpers');

beforeEach(() => {
mocked(createValidationMessage).mockName('createValidationMessage')
jest.mocked(createValidationMessage).mockName('createValidationMessage')
.mockImplementation((message) => message);
mocked(createValidatorResult).mockName('createValidatorResult')
jest.mocked(createValidatorResult).mockName('createValidatorResult')
.mockImplementation((error) => ({ error, message: 'message' }));
});

Expand Down
4 changes: 2 additions & 2 deletions src/validators/func.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import validateValue from './func';
import { createValidatorResult } from '../helpers';
import { mocked } from 'ts-jest/utils';


jest.mock('../helpers');

beforeEach(() => {
mocked(createValidatorResult).mockName('createValidatorResult')
jest.mocked(createValidatorResult).mockName('createValidatorResult')
.mockImplementation((error) => ({ error, message: 'message' }));
});

Expand Down
20 changes: 10 additions & 10 deletions src/validators/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import aggregator from './index';
import { VALIDATORS } from '../types';
import { mocked } from 'ts-jest/utils';


import required from './required';
import min from './min';
Expand All @@ -25,15 +25,15 @@ jest.mock('./func');
const validators = { required, min, max, email, url, postalCodeCA, sinCA, pattern, func };

beforeEach(() => {
mocked(required).mockName(VALIDATORS.required).mockReturnValue(VALIDATORS.required);
mocked(min).mockName(VALIDATORS.min).mockReturnValue(VALIDATORS.min);
mocked(max).mockName(VALIDATORS.max).mockReturnValue(VALIDATORS.max);
mocked(email).mockName(VALIDATORS.email).mockReturnValue(VALIDATORS.email);
mocked(url).mockName(VALIDATORS.url).mockReturnValue(VALIDATORS.url);
mocked(postalCodeCA).mockName(VALIDATORS.postalCodeCA).mockReturnValue(VALIDATORS.postalCodeCA);
mocked(sinCA).mockName(VALIDATORS.sinCA).mockReturnValue(VALIDATORS.sinCA);
mocked(pattern).mockName(VALIDATORS.pattern).mockReturnValue(VALIDATORS.pattern);
mocked(func).mockName(VALIDATORS.func).mockResolvedValue(VALIDATORS.func);
jest.mocked(required).mockName(VALIDATORS.required).mockReturnValue(VALIDATORS.required);
jest.mocked(min).mockName(VALIDATORS.min).mockReturnValue(VALIDATORS.min);
jest.mocked(max).mockName(VALIDATORS.max).mockReturnValue(VALIDATORS.max);
jest.mocked(email).mockName(VALIDATORS.email).mockReturnValue(VALIDATORS.email);
jest.mocked(url).mockName(VALIDATORS.url).mockReturnValue(VALIDATORS.url);
jest.mocked(postalCodeCA).mockName(VALIDATORS.postalCodeCA).mockReturnValue(VALIDATORS.postalCodeCA);
jest.mocked(sinCA).mockName(VALIDATORS.sinCA).mockReturnValue(VALIDATORS.sinCA);
jest.mocked(pattern).mockName(VALIDATORS.pattern).mockReturnValue(VALIDATORS.pattern);
jest.mocked(func).mockName(VALIDATORS.func).mockResolvedValue(VALIDATORS.func);
});

Object.values(VALIDATORS).forEach((name) => {
Expand Down
6 changes: 3 additions & 3 deletions src/validators/max.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import validateValue from './max';
import { createValidationMessage, createValidatorResult } from '../helpers';
import { VALIDATION_MESSAGES } from '../types';
import { mocked } from 'ts-jest/utils';


jest.mock('../helpers');

beforeEach(() => {
mocked(createValidationMessage).mockName('createValidationMessage')
jest.mocked(createValidationMessage).mockName('createValidationMessage')
.mockImplementation((message) => message);
mocked(createValidatorResult).mockName('createValidatorResult')
jest.mocked(createValidatorResult).mockName('createValidatorResult')
.mockImplementation((error) => ({ error, message: 'message' }));
});

Expand Down
6 changes: 3 additions & 3 deletions src/validators/min.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import validateValue from './min';
import { createValidationMessage, createValidatorResult } from '../helpers';
import { VALIDATION_MESSAGES } from '../types';
import { mocked } from 'ts-jest/utils';


jest.mock('../helpers');

beforeEach(() => {
mocked(createValidationMessage).mockName('createValidationMessage')
jest.mocked(createValidationMessage).mockName('createValidationMessage')
.mockImplementation((message) => message);
mocked(createValidatorResult).mockName('createValidatorResult')
jest.mocked(createValidatorResult).mockName('createValidatorResult')
.mockImplementation((error) => ({ error, message: 'message' }));
});

Expand Down
6 changes: 3 additions & 3 deletions src/validators/pattern.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import validateValue from './pattern';
import { createValidationMessage, createValidatorResult } from '../helpers';
import { VALIDATION_MESSAGES } from '../types';
import { mocked } from 'ts-jest/utils';


jest.mock('../helpers');

beforeEach(() => {
mocked(createValidationMessage).mockName('createValidationMessage')
jest.mocked(createValidationMessage).mockName('createValidationMessage')
.mockImplementation((message) => message);
mocked(createValidatorResult).mockName('createValidatorResult')
jest.mocked(createValidatorResult).mockName('createValidatorResult')
.mockImplementation((error) => ({ error, message: 'message' }));
});

Expand Down
6 changes: 3 additions & 3 deletions src/validators/postalCode-CA.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import validateValue from './postalCode-CA';
import { createValidationMessage, createValidatorResult } from '../helpers';
import { VALIDATION_MESSAGES } from '../types';
import { mocked } from 'ts-jest/utils';


jest.mock('../helpers');

beforeEach(() => {
mocked(createValidationMessage).mockName('createValidationMessage')
jest.mocked(createValidationMessage).mockName('createValidationMessage')
.mockImplementation((message) => message);
mocked(createValidatorResult).mockName('createValidatorResult')
jest.mocked(createValidatorResult).mockName('createValidatorResult')
.mockImplementation((error) => ({ error, message: 'message' }));
});

Expand Down
Loading

0 comments on commit 837b6fe

Please sign in to comment.