Skip to content

Instantly share code, notes, and snippets.

@Aziaev
Created November 2, 2018 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aziaev/26cbb50079f57d6df4fd0c1019dd95e4 to your computer and use it in GitHub Desktop.
Save Aziaev/26cbb50079f57d6df4fd0c1019dd95e4 to your computer and use it in GitHub Desktop.
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(() => {
});
}
};
@Aziaev
Copy link
Author

Aziaev commented Nov 2, 2018

describe('asyncValidate test', () => {
  const url = 'foobar';
  const initialState = {
    app: { url },
  };
  store.getState = jest.fn(() => initialState);

  describe('when returned values are have valid length', () => {
    const values = {
      lmsCourseGeneralInfo: {
        productISBN: 'aaaa5678910',
      },
    };
    const response = {
      response: {
        body: {
          isbn: 'AAAA5678910',
        },
      },
    };
    const getRequest = jest.fn(() => new Promise(resolve => resolve(response)));

    HTTPAsync.getRequest = getRequest;
    const expectedArg = [`${url}/wpng/api/v1/secure/products/search`, { productIsbn: 'AAAA5678910' }];

    it('', () => {
      return asyncValidate(values).then(data => {
        expect(getRequest).toBeCalledWith(...expectedArg);
        console.log(data);
      });
      // return asyncValidate(values).catch(e => expect(e).toMatch('error'));
    });
  });
});

@Aziaev
Copy link
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);
      });
    });
  });
});

@Aziaev
Copy link
Author

Aziaev commented Nov 2, 2018

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