Created
November 2, 2018 10:52
-
-
Save Aziaev/26cbb50079f57d6df4fd0c1019dd95e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const asyncValidate = (values) => { | |
if (values.lmsCourseGeneralInfo.productISBN && !ISBN_REGEXP.test(values.lmsCourseGeneralInfo.productISBN)) { | |
return sleep(100).then(() => { | |
// eslint-disable-next-line no-throw-literal | |
throw { | |
lmsCourseGeneralInfo: { | |
productISBN: VALIDATION_ERR_MESSAGE_PRODUCT_ID_FORMAT, | |
}, | |
}; | |
}); | |
} else if (values.lmsCourseGeneralInfo.productISBN) { | |
const url = getProductSearchApiUrl(); | |
const request = HTTPAsync.getRequest(url, { productIsbn: values.lmsCourseGeneralInfo.productISBN.toUpperCase() }); | |
return request | |
.then(response => { | |
if ( | |
response && | |
response.body && | |
response.body.isbn && | |
response.body.isbn.toUpperCase() !== values.lmsCourseGeneralInfo.productISBN.toUpperCase()) { | |
// eslint-disable-next-line no-throw-literal | |
throw { | |
lmsCourseGeneralInfo: { | |
productISBN: VALIDATION_ERR_MESSAGE_PRODUCT_ID_FORMAT, | |
}, | |
}; | |
} | |
}) | |
.catch(() => { | |
// eslint-disable-next-line no-throw-literal | |
throw { | |
lmsCourseGeneralInfo: { | |
productISBN: VALIDATION_ERR_MESSAGE_PRODUCT_ID_NOT_EXISTS, | |
}, | |
}; | |
}); | |
} else { | |
return sleep(100).then(() => { | |
}); | |
} | |
}; |
Author
Aziaev
commented
Nov 2, 2018
•
This works for error:
describe('asyncValidate test', () => {
const url = 'foobar';
const initialState = {
app: { url },
};
store.getState = jest.fn(() => initialState);
describe('when returned values are have invalid length', () => {
const values = {
lmsCourseGeneralInfo: {
productISBN: 'aaaa5',
},
};
const response = {
response: {
body: {
isbn: 'AAAA5678910',
},
},
};
HTTPAsync.getRequest = jest.fn(() => new Promise(resolve => resolve(response)));
it('Should return object with error', () => {
return asyncValidate(values).catch(e => {
const expectedError = { lmsCourseGeneralInfo: { productISBN: 'Invalid Product ISBN format' } };
expect(e).toMatchObject(expectedError);
});
});
});
});
Working test
describe('asyncValidate test', () => {
const url = 'foobar';
const initialState = {
app: { url },
};
store.getState = jest.fn(() => initialState);
describe('when values are have invalid length', () => {
const values = {
lmsCourseGeneralInfo: {
productISBN: 'aaaa5',
},
};
it('Should return object with error', () => {
HTTPAsync.getRequest = jest.fn(() => new Promise(resolve => resolve()));
return asyncValidate(values).catch(e => {
const expectedError = { lmsCourseGeneralInfo: { productISBN: 'Invalid Product ISBN format' } };
expect(e).toEqual(expectedError);
});
});
});
describe('when values have restricted symbols', () => {
const values = {
lmsCourseGeneralInfo: {
productISBN: 'жжжжж5678910',
},
};
const response = {
response: {
body: {
isbn: 'QQQQ5678910',
},
},
};
it('Should return Promise with error', () => {
HTTPAsync.getRequest = jest.fn(() => new Promise(resolve => resolve(response)));
return asyncValidate(values).catch(e => {
const expectedError = { lmsCourseGeneralInfo: { productISBN: 'Invalid Product ISBN format' } };
expect(e).toEqual(expectedError);
});
});
});
describe('when values are not found', () => {
const values = {
lmsCourseGeneralInfo: {
productISBN: 'qqqq5678910',
},
};
const response = {
body: {
isbn: 'QQQQ567891888',
},
};
it('Should return Promise with error', () => {
HTTPAsync.getRequest = jest.fn(() => new Promise(resolve => resolve(response)));
return asyncValidate(values).catch(data => {
const expectedError = { lmsCourseGeneralInfo: { productISBN: 'Product ISBN doesn\'t exists' } };
expect(data).toEqual(expectedError);
});
});
});
describe('when values are valid', () => {
const values = {
lmsCourseGeneralInfo: {
productISBN: 'qqqq5678910',
},
};
const response = {
body: {
isbn: 'QQQQ5678910',
},
};
it('Should return Promise with undefined resolve', () => {
HTTPAsync.getRequest = jest.fn(() => new Promise(resolve => resolve(response)));
return asyncValidate(values).then(data => {
expect(data).toBeUndefined();
});
});
});
describe('when values are undefined', () => {
const values = {
lmsCourseGeneralInfo: {},
};
const response = {
body: {
isbn: 'QQQQ5678910',
},
};
it('Should return Promise with undefined resolve', () => {
HTTPAsync.getRequest = jest.fn(() => new Promise(resolve => resolve(response)));
return asyncValidate(values).then(data => {
expect(data).toBeUndefined();
});
});
});
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment