|
| 1 | +import nock from 'nock' |
| 2 | +import { Duffel } from '../../index' |
| 3 | +import { MOCK_BOOKING, MOCK_CREATE_BOOKING_PAYLOAD } from '../mocks' |
| 4 | + |
| 5 | +const duffel = new Duffel({ token: 'mockToken' }) |
| 6 | +describe('Cars/Bookings', () => { |
| 7 | + afterEach(() => { |
| 8 | + nock.cleanAll() |
| 9 | + }) |
| 10 | + |
| 11 | + it('should post to /cars/bookings when `create` is called', async () => { |
| 12 | + const mockResponse = { data: MOCK_BOOKING } |
| 13 | + |
| 14 | + nock(/(.*)/) |
| 15 | + .post('/cars/bookings', (body) => { |
| 16 | + expect(body.data).toEqual(MOCK_CREATE_BOOKING_PAYLOAD) |
| 17 | + return true |
| 18 | + }) |
| 19 | + .reply(200, mockResponse) |
| 20 | + |
| 21 | + const response = await duffel.cars.bookings.create( |
| 22 | + MOCK_CREATE_BOOKING_PAYLOAD, |
| 23 | + ) |
| 24 | + expect(response.data).toEqual(mockResponse.data) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should get /cars/bookings/{id} when `get` is called', async () => { |
| 28 | + const bookingId = 'boo_0000Cx4Af0b5l45AT50eqO' |
| 29 | + const mockResponse = { data: MOCK_BOOKING } |
| 30 | + |
| 31 | + nock(/(.*)/).get(`/cars/bookings/${bookingId}`).reply(200, mockResponse) |
| 32 | + |
| 33 | + const response = await duffel.cars.bookings.get(bookingId) |
| 34 | + expect(response.data).toEqual(mockResponse.data) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should post to /cars/bookings/{id}/actions/cancel when `cancel` is called', async () => { |
| 38 | + const bookingId = 'boo_0000Cx4Af0b5l45AT50eqO' |
| 39 | + const mockResponse = { |
| 40 | + data: { |
| 41 | + ...MOCK_BOOKING, |
| 42 | + status: 'cancelled', |
| 43 | + cancelled_at: '2024-01-16T10:00:00Z', |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + nock(/(.*)/) |
| 48 | + .post(`/cars/bookings/${bookingId}/actions/cancel`) |
| 49 | + .reply(200, mockResponse) |
| 50 | + |
| 51 | + const response = await duffel.cars.bookings.cancel(bookingId) |
| 52 | + expect(response.data).toEqual(mockResponse.data) |
| 53 | + }) |
| 54 | +}) |
0 commit comments