|
| 1 | +import Command from '../../src/commands/command'; |
| 2 | +import { RetrieveTracking } from '../../src'; |
| 3 | +import { rest } from 'msw'; |
| 4 | +import { baseUrl } from '../fixtures/base-url'; |
| 5 | +import { |
| 6 | + invokerWithApiKey, |
| 7 | + invokerWithInvalidApiKey, |
| 8 | + invokerWithMissingApiKey, |
| 9 | +} from '../fixtures/invoker'; |
| 10 | +import { server } from '../fixtures/server'; |
| 11 | + |
| 12 | +describe('retrieve-tracking.ts', () => { |
| 13 | + it('should be able to instantiated', async () => { |
| 14 | + const command = new RetrieveTracking(); |
| 15 | + expect(command).toBeInstanceOf(Command); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('when API_KEY is not provided', () => { |
| 19 | + it('should returns authorization failure', async () => { |
| 20 | + const payload = { |
| 21 | + success: true, |
| 22 | + error: 'Authorization failed', |
| 23 | + code: 40101001, |
| 24 | + }; |
| 25 | + |
| 26 | + server.use( |
| 27 | + rest.get(baseUrl('trackings/1'), (_, res, ctx) => { |
| 28 | + return res(ctx.status(401), ctx.json(payload)); |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + const command = new RetrieveTracking({ id: 1 }); |
| 33 | + |
| 34 | + const response = await invokerWithMissingApiKey.send(command); |
| 35 | + |
| 36 | + expect(response).toEqual(payload); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('when API_KEY is invalid', () => { |
| 41 | + it('should return invalid api key warnings', async () => { |
| 42 | + const payload = { |
| 43 | + success: false, |
| 44 | + error: |
| 45 | + 'Authentication for your key is failed. Please make sure input the right key or contact info@biteship.com for more information.', |
| 46 | + code: 40000001, |
| 47 | + }; |
| 48 | + |
| 49 | + server.use( |
| 50 | + rest.get(baseUrl('trackings/1'), (_, res, ctx) => { |
| 51 | + return res(ctx.status(400), ctx.json(payload)); |
| 52 | + }), |
| 53 | + ); |
| 54 | + |
| 55 | + const command = new RetrieveTracking({ id: 1 }); |
| 56 | + |
| 57 | + const response = await invokerWithInvalidApiKey.send(command); |
| 58 | + |
| 59 | + expect(response).toEqual(payload); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('when tracking found', () => { |
| 64 | + it('should returns list of current tracking histories', async () => { |
| 65 | + const payload = { |
| 66 | + success: true, |
| 67 | + messsage: 'Successfully get tracking info', |
| 68 | + object: 'tracking', |
| 69 | + id: '6051861741a37414e6637fab', |
| 70 | + waybill_id: '0123082100003094', |
| 71 | + courier: { |
| 72 | + company: 'jne', |
| 73 | + name: null, |
| 74 | + phone: null, |
| 75 | + }, |
| 76 | + origin: { |
| 77 | + contact_name: '[INSTANT COURIER] BITESHIP/FIE', |
| 78 | + address: |
| 79 | + 'JALAN TANJUNG 16 NO.5, RT.8/RW.2, WEST TANJUNG, SOUTH JAKARTA CITY, JAKARTA, IN', |
| 80 | + }, |
| 81 | + destination: { |
| 82 | + contact_name: 'ADITARA MADJID', |
| 83 | + address: |
| 84 | + 'THE PAKUBUWONO RESIDENCE, JALAN PAKUBUWONO VI, RW.1, GUNUNG, SOUTH JAKARTA CITY', |
| 85 | + }, |
| 86 | + history: [ |
| 87 | + { |
| 88 | + note: 'SHIPMENT RECEIVED BY JNE COUNTER OFFICER AT [JAKARTA]', |
| 89 | + updated_at: '2021-03-16T18:17:00+07:00', |
| 90 | + status: 'dropping_off', |
| 91 | + }, |
| 92 | + { |
| 93 | + note: 'RECEIVED AT SORTING CENTER [JAKARTA]', |
| 94 | + updated_at: '2021-03-16T21:15:00+07:00', |
| 95 | + status: 'dropping_off', |
| 96 | + }, |
| 97 | + { |
| 98 | + note: 'SHIPMENT FORWARDED TO DESTINATION [JAKARTA , HUB VETERAN BINTARO]', |
| 99 | + updated_at: '2021-03-16T23:12:00+07:00', |
| 100 | + status: 'dropping_off', |
| 101 | + }, |
| 102 | + { |
| 103 | + note: 'RECEIVED AT INBOUND STATION [JAKARTA , HUB VETERAN BINTARO]', |
| 104 | + updated_at: '2021-03-16T23:43:00+07:00', |
| 105 | + status: 'dropping_off', |
| 106 | + }, |
| 107 | + { |
| 108 | + note: 'WITH DELIVERY COURIER [JAKARTA , HUB VETERAN BINTARO]', |
| 109 | + updated_at: '2021-03-17T09:29:00+07:00', |
| 110 | + status: 'dropping_off', |
| 111 | + }, |
| 112 | + { |
| 113 | + note: 'DELIVERED TO [ainul yakin | 17-03-2021 11:15 | JAKARTA ]', |
| 114 | + updated_at: '2021-03-17T11:15:00+07:00', |
| 115 | + status: 'delivered', |
| 116 | + }, |
| 117 | + ], |
| 118 | + link: '-', |
| 119 | + order_id: '6251863341sa3714e6637fab', |
| 120 | + status: 'delivered', |
| 121 | + }; |
| 122 | + |
| 123 | + const params = { |
| 124 | + id: 1, |
| 125 | + }; |
| 126 | + |
| 127 | + server.use( |
| 128 | + rest.get(baseUrl('trackings/1'), (_, res, ctx) => { |
| 129 | + return res(ctx.json(payload)); |
| 130 | + }), |
| 131 | + ); |
| 132 | + |
| 133 | + const command = new RetrieveTracking(params); |
| 134 | + |
| 135 | + const response = await invokerWithApiKey.send(command); |
| 136 | + |
| 137 | + expect(response).toEqual(payload); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments