Skip to content

Commit

Permalink
fix: fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
qiqiboy committed Nov 12, 2020
1 parent ea17b51 commit f3a9f71
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 13 deletions.
2 changes: 1 addition & 1 deletion jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
},
moduleFileExtensions: ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'],
verbose: true,
resetMocks: true,
// resetMocks: true,
watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname']
}
]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"start": "cd docs && npm start",
"build": "npm run clear && rollup -c",
"clear": "rimraf dist",
"test": "node jest/test.js -c jest/jest.config.js"
"test": "node jest/test.js"
},
"repository": {
"type": "git",
Expand Down
33 changes: 26 additions & 7 deletions tests/EasyField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ describe('native browser field', () => {
text: '123',
custom: '123'
});

expect(getFormutil().$getField('text')!.$value).toBe('123');
expect(getByPlaceholderText('text')).toHaveValue('123');
expect(getByPlaceholderText('custom')).toHaveValue('123');

userEvent.type(getByPlaceholderText('password'), '456');

expect(getFormutil().$params).toEqual({
text: '123',
password: '123456',
Expand Down Expand Up @@ -88,6 +90,7 @@ describe('native browser field', () => {
expect(getByTestId('radio')).toBeChecked();

userEvent.click(getByTestId('checkbox'));

expect(getFormutil().$params).toEqual({
checkbox: true,
radio: true
Expand Down Expand Up @@ -117,6 +120,7 @@ describe('native browser field', () => {
expect(getByTestId('a1')).toBeChecked();

userEvent.click(getByTestId('a2'));

expect(getFormutil().$params).toEqual({
group: [1, 2]
});
Expand Down Expand Up @@ -157,6 +161,7 @@ describe('native browser field', () => {
required: 'required!'
}
});

expect(getFormutil().$params).toEqual({
list: []
});
Expand Down Expand Up @@ -232,12 +237,14 @@ describe('custom component field', () => {

expect(getByTestId('input').value).toBe('1');
expect(getByTestId('input-2').value).toBe('2');

expect(getFormutil().$params).toEqual({
a: getByTestId('input').value,
b: getByTestId('input-2').value
});

userEvent.type(getByTestId('input'), '2');

expect(getFormutil().$params).toEqual({
a: '12',
b: '2'
Expand Down Expand Up @@ -293,13 +300,14 @@ describe('built-in validators', () => {

userEvent.type(getByTestId('input'), '1'); // 111
userEvent.type(getByTestId('input-2'), '1'); // 111

expect(getFormutil().$errors).toEqual({
a: { max: 'Error input: max' },
b: { max: 'Error input: max' }
});
});

test('maxLength/minLength', () => {
test('maxLength/minLength', async () => {
const { getFormutil, getByTestId } = renderForm(
<>
<EasyField name="a" data-testid="input" required $defaultValue="" minLength={2} maxLength={5} />
Expand All @@ -315,12 +323,12 @@ describe('built-in validators', () => {
userEvent.type(getByTestId('input-2'), '123'); // 12
expect(getFormutil().$errors).toEqual({});

userEvent.type(getByTestId('input'), '456'); // 123456
userEvent.type(getByTestId('input-2'), '456'); // 123456
expect(getFormutil().$errors).toEqual({
a: { maxLength: 'Error input: maxLength' },
b: { maxLength: 'Error input: maxLength' }
});
// 由于新版的jsdom支持了maxLength,所以上面的输入模拟无法输入超过maxLength的值
// 我们期望输入456后最终得到123456的表单值,但是实际上只会是12345
userEvent.type(getByTestId('input'), '456'); // 12345
userEvent.type(getByTestId('input-2'), '456'); // 12345

expect(getFormutil().$errors).toEqual({});
});

test('pattern', () => {
Expand Down Expand Up @@ -368,16 +376,19 @@ describe('built-in validators', () => {

expect(checkerSpy).toBeCalledTimes(1);
expect(checkerSpy2).toBeCalledTimes(1);

expect(getFormutil().$errors).toEqual({
a: { checker: 'Error input: checker' },
b: { checker: 'Error input: checker' }
});

userEvent.type(getByTestId('input'), '1'); // 11
userEvent.type(getByTestId('input-2'), '1'); // 1

expect(getFormutil().$errors).toEqual({
b: { checker: 'Error input: checker' }
});

expect(checkerSpy).toBeCalledTimes(2);
expect(checkerSpy2).toBeCalledTimes(2);
});
Expand All @@ -404,6 +415,7 @@ describe('built-in validators', () => {

userEvent.type(getByTestId('input'), '1'); // 1
userEvent.type(getByTestId('input-2'), '1'); // 1

expect(getFormutil().$errors).toEqual({
a: { minLength: 'Error input: minLength' },
b: { minLength: 'Error input: minLength' }
Expand All @@ -424,6 +436,7 @@ describe('checked / unchecked', () => {
});

userEvent.click(getByTestId('input'));

expect(getFormutil().$params).toEqual({
a: 'yes'
});
Expand Down Expand Up @@ -454,6 +467,7 @@ describe('validMessage', () => {
});

userEvent.type(getByTestId('input'), '1');

expect(getFormutil().$errors).toEqual({
a: {
minLength: 'Your name should be more than 5 characters'
Expand All @@ -465,6 +479,7 @@ describe('validMessage', () => {
describe('valuePropName / changePropName / focusPropName / blurPropName', () => {
test('change fieldHandler', () => {
let fieldHandler;

renderForm(
<>
<EasyField
Expand All @@ -475,6 +490,7 @@ describe('valuePropName / changePropName / focusPropName / blurPropName', () =>
blurPropName="onLeave"
render={a => {
fieldHandler = a;

return null;
}}
/>
Expand All @@ -500,6 +516,7 @@ describe('passUtil', () => {
passUtil
render={a => {
fieldHandler = a;

return null;
}}
/>
Expand All @@ -515,6 +532,7 @@ describe('passUtil', () => {
passUtil="fieldutilAlias"
render={a => {
fieldHandler = a;

return null;
}}
/>
Expand Down Expand Up @@ -544,6 +562,7 @@ describe('getValueFromEvent()', () => {
});

userEvent.type(getByTestId('input'), '1');

expect(getFormutil().$params).toEqual({
a: '1--'
});
Expand Down
15 changes: 12 additions & 3 deletions tests/Field.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ describe('name', () => {
expect($formutil.$params.a.b[1].c[2].d).toBe('');
});

test('should show wanrings when missing name ', () => {
test('should show wanrings when missing name', () => {
renderForm(<Field children={null} />);

expect(console.error).toHaveBeenLastCalledWith(
'Warning: You should assign a name to <Field />, otherwise it will be isolated!'
);
});

test('should show wanrings if not underneath a Form ', () => {
test('should show wanrings if not underneath a Form', () => {
render(<Field name="a" children={null} />);

expect(console.error).toHaveBeenLastCalledWith(
Expand Down Expand Up @@ -115,6 +115,7 @@ describe('$validators', () => {
const $validators = {
syncValidate: (value) => {
spyValidators.sync();

return !!value || 'sync-validate!';
},
asyncValidate: jest.fn((value) => {
Expand Down Expand Up @@ -144,6 +145,7 @@ describe('$validators', () => {
rerender({
asyncValidate: true
});

getFieldutil().$render('');

expect(spyValidators.sync).toBeCalledTimes(2);
Expand All @@ -164,6 +166,7 @@ describe('$validators', () => {
expect(getFieldutil().$error).toEqual({
asyncValidate: new Error('async-validate!')
});

expect(spyValidators.sync).toBeCalledTimes(3);
expect(spyValidators.async).toBeCalledTimes(2);
});
Expand Down Expand Up @@ -198,6 +201,7 @@ describe('$validateLazy', () => {
rerender({
$validateLazy: true
});

getFieldutil().$render('');
expect($validators.syncValidate).toBeCalledTimes(2);
expect($validators.asyncValidate).toBeCalledTimes(1);
Expand Down Expand Up @@ -328,6 +332,7 @@ describe('$memo', () => {
a: 1,
b: 1
});

expect(renderA).toBeCalledTimes(2);
expect(renderB).toBeCalledTimes(2);
});
Expand Down Expand Up @@ -404,7 +409,7 @@ describe('$memo', () => {
describe('$onFieldChange()', () => {
test('called when field value change', async () => {
const onChange = jest.fn();
const { getFieldutil, getFormutil, getElement } = renderField({
const { getFormutil, getElement } = renderField({
name: 'a',
$onFieldChange: onChange
});
Expand Down Expand Up @@ -458,6 +463,7 @@ describe('$fieldutil', () => {
const input = getByTestId('input');

fireEvent.focus(input);

fireEvent.input(input, {
target: { value: 'b' }
});
Expand All @@ -475,6 +481,7 @@ describe('$fieldutil', () => {
getFieldutil().$$formutil.$setValues({
a: 'c'
});

expect(input.value).toBe('c');
});

Expand Down Expand Up @@ -552,12 +559,14 @@ describe('$fieldutil', () => {
},
callback
);

getFieldutil().$setValidity('maxlength', 'maxlength < 5', callback);

expect(getFieldutil().$error).toEqual({
required: 'required!',
maxlength: 'maxlength < 5'
});

expect(callback).toBeCalled();
expect(callback.mock.calls[1][0]).toBe(getFieldutil());
});
Expand Down
Loading

0 comments on commit f3a9f71

Please sign in to comment.