@stylusapparel/stylusop-api-node-wrapper
Version:
This is the official NodeJs wrapper for connecting to the StylusOP API
82 lines (78 loc) • 2.42 kB
JavaScript
const axios = require('axios');
const __mockWrapper = require('..');
const { __defaults } = require('../constants/url');
const { _errorConst } = require('../constants/const');
const { MOCK_SECRET_TOKEN, MOCK_VENDOR_NAME, IS_TOKEN__VALID_SUCCESS_RESPONSE, IS_TOKEN__VALID_ERROR_RESPONSE, IS_TOKEN__VALID_EXPIRE_ERROR_RESPONSE } = require('./__mocks__/auth.json');
const _mockClient = __mockWrapper.createClient(MOCK_SECRET_TOKEN, { apiVersion: __defaults.LATEST_VERSION, sandBox: true, username: MOCK_VENDOR_NAME });
afterEach(() => {
jest.clearAllMocks();
});
describe('AUTH MODULE - Validating success scenarios', () => {
it(' => stylusClient.isTokenValid() ', async () => {
axios.post.mockImplementationOnce(() =>
Promise.resolve({
data: { ...IS_TOKEN__VALID_SUCCESS_RESPONSE },
status: 200,
}),
);
const gen = await _mockClient.isTokenValid();
expect(axios.post).toHaveBeenCalledTimes(1);
expect(gen).toEqual(true);
});
});
describe('AUTH MODULE - Validating failure scenario', () => {
it(' => stylusClient.isTokenValid() - Token Invalid', async () => {
axios.post.mockImplementationOnce(() =>
Promise.reject({
response: {
status: 401,
data: {
...IS_TOKEN__VALID_ERROR_RESPONSE,
},
},
}),
);
try {
await _mockClient.isTokenValid();
expect(true).toEqual(false);
} catch (err) {
expect(axios.post).toHaveBeenCalledTimes(1);
expect(err.errorCode).toEqual(_errorConst.TOKEN_INVALID);
}
});
it(' => stylusClient.isTokenValid() - Token Expire', async () => {
axios.post.mockImplementationOnce(() =>
Promise.reject({
response: {
status: 401,
data: {
...IS_TOKEN__VALID_EXPIRE_ERROR_RESPONSE,
},
},
}),
);
try {
await _mockClient.isTokenValid();
expect(true).toEqual(false);
} catch (err) {
expect(axios.post).toHaveBeenCalledTimes(1);
expect(err.errorCode).toEqual(_errorConst.TOKEN_EXPIRE);
}
});
it(' => stylusClient.isToken() - Unknown error', async () => {
axios.post.mockImplementationOnce(() =>
Promise.reject({
response: {
status: 500,
},
}),
);
try {
await _mockClient.isTokenValid();
expect(true).toEqual(false);
} catch (err) {
expect(axios.post).toHaveBeenCalledTimes(1);
expect(err.errorCode).toEqual(_errorConst.UNKNOWN_ERROR);
}
});
});