UNPKG

@stylusapparel/opv3-merchant-api-nodejs

Version:

This is the official NodeJs wrapper for connecting to the StylusOP API V3

92 lines (88 loc) 3.38 kB
const axios = require('axios'); const { __defaults } = require('../constants/url'); const __mockWrapper = require('..'); const { MOCK_SECRET_TOKEN, MOCK_MERCHANT_ID, MOCK_MERCHANT_NAME } = require('./__mocks__/auth.json'); const { MOCK_LIST_PRODUCTS_RESPONSE, MOCK_SEARCH_PRODUCT, MOCK_PRODUCT_ID, MOCK_GET_PRODUCT_RESPONSE } = require('./__mocks__/product.json'); const { _errorConst } = require("../constants/const"); const {OAUTH_TOKEN_RESPONSE} = require("./__mocks__/auth.json"); // Set the API URL to the local API for testing in development process.env.STYLUSOP_API_URL = 'http://localhost:3600/api/'; const _mockClient = __mockWrapper.createClient(MOCK_MERCHANT_ID, MOCK_SECRET_TOKEN, { apiVersion: __defaults.LATEST_VERSION, sandbox: true, merchantName: MOCK_MERCHANT_NAME, maxRetries: 0 } ); afterEach(() => { jest.clearAllMocks(); }); beforeEach(async () => { axios.post.mockImplementationOnce(() => Promise.resolve({ data: { ...OAUTH_TOKEN_RESPONSE }, status: 200, }), ); await _mockClient.oauthToken(); }); describe('PRODUCT MODULE - Validating success scenarios', () => { it(' => stylusClient.products.list({SEARCH_OBJECT}) ', async () => { axios.get.mockImplementationOnce(() => Promise.resolve({ data: { ...MOCK_LIST_PRODUCTS_RESPONSE }, status: 200, }), ); const gen = await _mockClient.products.list(MOCK_SEARCH_PRODUCT); expect(axios.get).toHaveBeenCalledTimes(1); expect(gen).toEqual(MOCK_LIST_PRODUCTS_RESPONSE.products); }); it(' => stylusClient.products.get(MOCK_PRODUCT_ID) ', async () => { axios.get.mockImplementationOnce(() => Promise.resolve({ data: { ...MOCK_GET_PRODUCT_RESPONSE }, status: 200, }), ); const gen = await _mockClient.products.get(MOCK_PRODUCT_ID); expect(axios.get).toHaveBeenCalledTimes(1); expect(gen).toEqual(MOCK_GET_PRODUCT_RESPONSE.product); }); }); describe("PRODUCT MODULE - Validating failure scenarios", () => { it(" => stylusClient.products.list() ", async () => { axios.get.mockImplementationOnce(() => Promise.reject({ response: { data: { error: { errorCode: _errorConst.UNKNOWN_ERROR } }, status: 500 } })); try { await _mockClient.products.list(); expect(true).toEqual(false); } catch (err) { expect(axios.get).toHaveBeenCalledTimes(1); expect(err.errorCode).toEqual(_errorConst.UNKNOWN_ERROR); } }); it(" => stylusClient.products.get(MOCK_PRODUCT_ID) ", async () => { axios.get.mockImplementationOnce(() => Promise.reject({ response: { data: { error: { errorCode: "ProductNotFound" } }, status: 404 } })); try { await _mockClient.products.get(MOCK_PRODUCT_ID); expect(true).toEqual(false); } catch (err) { expect(axios.get).toHaveBeenCalledTimes(1); expect(err.errorCode).toEqual(_errorConst.PRODUCT_NOT_FOUND); } }); })