|
| 1 | +import Command from '../../src/commands/command'; |
| 2 | +import { DeleteOrder } 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('create-order.ts', () => { |
| 13 | + it('should be able to instantiated', async () => { |
| 14 | + const command = new DeleteOrder(); |
| 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.delete(baseUrl('orders/1'), (_, res, ctx) => { |
| 28 | + return res(ctx.status(401), ctx.json(payload)); |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + const command = new DeleteOrder({ 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.delete(baseUrl('orders/1'), (_, res, ctx) => { |
| 51 | + return res(ctx.status(400), ctx.json(payload)); |
| 52 | + }), |
| 53 | + ); |
| 54 | + |
| 55 | + const command = new DeleteOrder({ id: 1 }); |
| 56 | + |
| 57 | + const response = await invokerWithInvalidApiKey.send(command); |
| 58 | + |
| 59 | + expect(response).toEqual(payload); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('when order successfully deleted', () => { |
| 64 | + it('should returns 200 OK', async () => { |
| 65 | + const payload = { |
| 66 | + success: true, |
| 67 | + message: 'Order successfully deleted', |
| 68 | + object: 'order', |
| 69 | + id: '5dd5a396248481164a225af4', |
| 70 | + status: 'cancelled', |
| 71 | + cancellation_reason: 'Ingin mengganti kurir', |
| 72 | + }; |
| 73 | + |
| 74 | + server.use( |
| 75 | + rest.delete(baseUrl('orders/1'), (_, res, ctx) => { |
| 76 | + return res(ctx.status(200), ctx.json(payload)); |
| 77 | + }), |
| 78 | + ); |
| 79 | + |
| 80 | + const command = new DeleteOrder({ |
| 81 | + id: 1, |
| 82 | + cancelllation_reason: 'Ingin mengganti kurir', |
| 83 | + }); |
| 84 | + |
| 85 | + const response = await invokerWithApiKey.send(command); |
| 86 | + |
| 87 | + expect(response).toEqual(payload); |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments