|
| 1 | +/* * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 2 | +/* Copyright (c) 2019 Mobify Research & Development Inc. All rights reserved. */ |
| 3 | +/* * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 4 | + |
| 5 | +import expect from 'expect.js' |
| 6 | +import sinon from 'sinon' |
| 7 | + |
| 8 | +import ShopApi from '../../src/index' |
| 9 | +import {clientId, baseUrl} from '../config.json' |
| 10 | + |
| 11 | +let client |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + client = new ShopApi.ApiClient({ |
| 15 | + basePath: `${baseUrl}`, |
| 16 | + defaultHeaders: {'x-dw-client-id': clientId} |
| 17 | + }) |
| 18 | +}) |
| 19 | + |
| 20 | +describe('ApiClient', () => { |
| 21 | + describe('sendApiRequest', () => { |
| 22 | + let mockRequest |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + mockRequest = {end: sinon.stub()} |
| 26 | + }) |
| 27 | + |
| 28 | + it('should deserialize a successful response into a string', () => { |
| 29 | + const mockData = Symbol() |
| 30 | + const mockResponse = Symbol() |
| 31 | + |
| 32 | + client.deserialize = sinon.stub().returns(mockData) |
| 33 | + mockRequest.end.callsArgWith(0, null, mockResponse) |
| 34 | + |
| 35 | + return client.sendApiRequest(mockRequest, 'String') |
| 36 | + .then((result) => { |
| 37 | + expect(result.data).to.be(mockData) |
| 38 | + expect(result.response).to.be(mockResponse) |
| 39 | + expect(client.deserialize.calledWith(mockResponse, 'String')).to.be(true) |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should deserialize the response into a blob', () => { |
| 44 | + const mockData = Symbol() |
| 45 | + const mockResponse = Symbol() |
| 46 | + |
| 47 | + client.deserialize = sinon.stub().returns(mockData) |
| 48 | + mockRequest.end.callsArgWith(0, null, mockResponse) |
| 49 | + |
| 50 | + return client.sendApiRequest(mockRequest, 'Blob') |
| 51 | + .then((result) => { |
| 52 | + expect(result.data).to.be(mockData) |
| 53 | + expect(result.response).to.be(mockResponse) |
| 54 | + expect(client.deserialize.calledWith(mockResponse, 'Blob')).to.be(true) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should reject when deserializing the response throws an error', () => { |
| 59 | + const mockError = Symbol() |
| 60 | + |
| 61 | + client.deserialize = sinon.stub().throws(mockError) |
| 62 | + mockRequest.end.callsArg(0) |
| 63 | + |
| 64 | + return client.sendApiRequest(mockRequest, 'String') |
| 65 | + .catch((error) => { |
| 66 | + expect(error).to.be(mockError) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should reject when the request fails with valid JSON', () => { |
| 71 | + const mockError = { |
| 72 | + response: { |
| 73 | + text: '{ "fault": { "message": "foo", "type": "bar" } }' |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + mockRequest.end.callsArgWith(0, mockError) |
| 78 | + |
| 79 | + return client.sendApiRequest(mockRequest, 'String') |
| 80 | + .catch((error) => { |
| 81 | + expect(error).to.be.a(ShopApi.Fault) |
| 82 | + expect(error.message).to.equal('foo') |
| 83 | + expect(error.type).to.equal('bar') |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should reject when the request fails with HTML', () => { |
| 88 | + const mockError = { |
| 89 | + response: { |
| 90 | + text: '<html><head></head><body></body></html>' |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + mockRequest.end.callsArgWith(0, mockError) |
| 95 | + |
| 96 | + return client.sendApiRequest(mockRequest, 'String') |
| 97 | + .catch((error) => { |
| 98 | + expect(error).to.be.a(SyntaxError) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + it('should reject when the request fails for any reason', () => { |
| 103 | + const mockError = Symbol() |
| 104 | + |
| 105 | + mockRequest.end.callsArgWith(0, mockError) |
| 106 | + |
| 107 | + return client.sendApiRequest(mockRequest, 'String') |
| 108 | + .catch((error) => { |
| 109 | + expect(error).to.be(mockError) |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments