|
1 |
| -import { describe, expect, it } from 'vitest'; |
| 1 | +import { fireEvent, render, screen, within } from '@testing-library/react'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
2 | 3 |
|
3 | 4 | import {
|
| 5 | + DeliveryAreaPickupTaskDefinition, |
4 | 6 | deliveryCustomInsertCartId,
|
5 | 7 | deliveryCustomInsertDropoff,
|
6 | 8 | deliveryCustomInsertOnCancel,
|
7 | 9 | deliveryCustomInsertPickup,
|
8 | 10 | DeliveryCustomTaskDescription,
|
| 11 | + DeliveryCustomTaskForm, |
9 | 12 | deliveryInsertCartId,
|
10 | 13 | deliveryInsertDropoff,
|
11 | 14 | deliveryInsertOnCancel,
|
12 | 15 | deliveryInsertPickup,
|
| 16 | + DeliveryPickupTaskDefinition, |
13 | 17 | DeliveryPickupTaskDescription,
|
| 18 | + DeliveryPickupTaskForm, |
| 19 | + DeliverySequentialLotPickupTaskDefinition, |
| 20 | + isDeliveryCustomTaskDescriptionValid, |
| 21 | + isDeliveryPickupTaskDescriptionValid, |
14 | 22 | makeDefaultDeliveryCustomTaskDescription,
|
15 | 23 | makeDefaultDeliveryPickupTaskDescription,
|
| 24 | + makeDeliveryCustomTaskBookingLabel, |
| 25 | + makeDeliveryCustomTaskShortDescription, |
| 26 | + makeDeliveryPickupTaskBookingLabel, |
| 27 | + makeDeliveryPickupTaskShortDescription, |
16 | 28 | } from '.';
|
17 | 29 |
|
| 30 | +const mockPickupPoints = { |
| 31 | + pickup_1: 'handler_1', |
| 32 | + pickup_2: 'handler_2', |
| 33 | +}; |
| 34 | +const mockCartIds = ['cart_1', 'cart_2', 'cart_3']; |
| 35 | +const mockDropoffPoints = { |
| 36 | + dropoff_1: 'handler_3', |
| 37 | + dropoff_2: 'handler_4', |
| 38 | +}; |
| 39 | +const mockPickupZones = { |
| 40 | + pickup_1: 'zone_1', |
| 41 | + pickup_2: 'zone_2', |
| 42 | +}; |
| 43 | + |
18 | 44 | describe('Custom deliveries', () => {
|
19 | 45 | it('delivery pickup', () => {
|
20 | 46 | let deliveryPickupTaskDescription: DeliveryPickupTaskDescription | null = null;
|
@@ -133,6 +159,89 @@ describe('Custom deliveries', () => {
|
133 | 159 | expect(deliveryPickupTaskDescription).toEqual(description);
|
134 | 160 | });
|
135 | 161 |
|
| 162 | + it('delivery pickup task form renders, changes and validates', async () => { |
| 163 | + const onChange = vi.fn(); |
| 164 | + const onValidate = vi.fn(); |
| 165 | + |
| 166 | + render( |
| 167 | + <DeliveryPickupTaskForm |
| 168 | + taskDesc={makeDefaultDeliveryPickupTaskDescription()} |
| 169 | + pickupPoints={mockPickupPoints} |
| 170 | + cartIds={mockCartIds} |
| 171 | + dropoffPoints={mockDropoffPoints} |
| 172 | + onChange={onChange} |
| 173 | + onValidate={onValidate} |
| 174 | + />, |
| 175 | + ); |
| 176 | + |
| 177 | + let triggerCount = 1; |
| 178 | + |
| 179 | + const pickupPlace = screen.getByTestId('pickup-location'); |
| 180 | + let input = within(pickupPlace).getByLabelText(/pickup location/i); |
| 181 | + pickupPlace.focus(); |
| 182 | + fireEvent.change(input, { target: { value: 'a' } }); |
| 183 | + fireEvent.keyDown(pickupPlace, { key: 'ArrowDown' }); |
| 184 | + fireEvent.keyDown(pickupPlace, { key: 'Enter' }); |
| 185 | + expect(onChange).toHaveBeenCalledTimes(triggerCount); |
| 186 | + expect(onValidate).toHaveBeenCalledTimes(triggerCount); |
| 187 | + triggerCount += 1; |
| 188 | + |
| 189 | + const cartId = screen.getByTestId('cart-id'); |
| 190 | + cartId.focus(); |
| 191 | + input = within(cartId).getByLabelText(/cart id/i); |
| 192 | + fireEvent.change(input, { target: { value: 'a' } }); |
| 193 | + fireEvent.keyDown(pickupPlace, { key: 'ArrowDown' }); |
| 194 | + fireEvent.keyDown(pickupPlace, { key: 'Enter' }); |
| 195 | + expect(onChange).toHaveBeenCalledTimes(triggerCount); |
| 196 | + expect(onValidate).toHaveBeenCalledTimes(triggerCount); |
| 197 | + triggerCount += 1; |
| 198 | + |
| 199 | + const dropoffPlace = screen.getByTestId('dropoff-location'); |
| 200 | + dropoffPlace.focus(); |
| 201 | + input = within(dropoffPlace).getByLabelText(/dropoff location/i); |
| 202 | + fireEvent.change(input, { target: { value: 'a' } }); |
| 203 | + fireEvent.keyDown(dropoffPlace, { key: 'ArrowDown' }); |
| 204 | + fireEvent.keyDown(dropoffPlace, { key: 'Enter' }); |
| 205 | + expect(onChange).toHaveBeenCalledTimes(triggerCount); |
| 206 | + expect(onValidate).toHaveBeenCalledTimes(triggerCount); |
| 207 | + triggerCount += 1; |
| 208 | + }); |
| 209 | + |
| 210 | + it('delivery pickup booking label', () => { |
| 211 | + let desc = makeDefaultDeliveryPickupTaskDescription(); |
| 212 | + desc = deliveryInsertPickup(desc, 'test_place', 'test_lot'); |
| 213 | + desc = deliveryInsertCartId(desc, 'test_cart_id'); |
| 214 | + desc = deliveryInsertDropoff(desc, 'test_dropoff'); |
| 215 | + const label = makeDeliveryPickupTaskBookingLabel(desc); |
| 216 | + expect(label.task_definition_id).toBe(DeliveryPickupTaskDefinition.taskDefinitionId); |
| 217 | + expect(label.pickup).toBe('test_lot'); |
| 218 | + expect(label.destination).toBe('test_dropoff'); |
| 219 | + expect(label.cart_id).toBe('test_cart_id'); |
| 220 | + }); |
| 221 | + |
| 222 | + it('delivery pickup validity', () => { |
| 223 | + let desc = makeDefaultDeliveryPickupTaskDescription(); |
| 224 | + expect( |
| 225 | + isDeliveryPickupTaskDescriptionValid(desc, mockPickupPoints, mockDropoffPoints), |
| 226 | + ).not.toBeTruthy(); |
| 227 | + desc = deliveryInsertPickup(desc, 'pickup_1', 'handler_1'); |
| 228 | + desc = deliveryInsertCartId(desc, 'cart_1'); |
| 229 | + desc = deliveryInsertDropoff(desc, 'dropoff_1'); |
| 230 | + expect( |
| 231 | + isDeliveryPickupTaskDescriptionValid(desc, mockPickupPoints, mockDropoffPoints), |
| 232 | + ).toBeTruthy(); |
| 233 | + }); |
| 234 | + |
| 235 | + it('delivery pickup short description', () => { |
| 236 | + let desc = makeDefaultDeliveryPickupTaskDescription(); |
| 237 | + desc = deliveryInsertPickup(desc, 'pickup_1', 'handler_1'); |
| 238 | + desc = deliveryInsertCartId(desc, 'cart_1'); |
| 239 | + desc = deliveryInsertDropoff(desc, 'dropoff_1'); |
| 240 | + expect(makeDeliveryPickupTaskShortDescription(desc, undefined)).toBe( |
| 241 | + '[Delivery - 1:1] payload [cart_1] from [pickup_1] to [dropoff_1]', |
| 242 | + ); |
| 243 | + }); |
| 244 | + |
136 | 245 | it('delivery_sequential_lot_pickup', () => {
|
137 | 246 | let deliveryCustomTaskDescription: DeliveryCustomTaskDescription | null = null;
|
138 | 247 | try {
|
@@ -369,4 +478,107 @@ describe('Custom deliveries', () => {
|
369 | 478 | ]);
|
370 | 479 | expect(deliveryCustomTaskDescription).toEqual(description);
|
371 | 480 | });
|
| 481 | + |
| 482 | + it('delivery custom task form renders, changes and validates', async () => { |
| 483 | + const onChange = vi.fn(); |
| 484 | + const onValidate = vi.fn(); |
| 485 | + |
| 486 | + render( |
| 487 | + <DeliveryCustomTaskForm |
| 488 | + taskDesc={makeDefaultDeliveryCustomTaskDescription('delivery_sequential_lot_pickup')} |
| 489 | + pickupZones={Object.keys(mockPickupZones)} |
| 490 | + cartIds={mockCartIds} |
| 491 | + dropoffPoints={Object.keys(mockDropoffPoints)} |
| 492 | + onChange={onChange} |
| 493 | + onValidate={onValidate} |
| 494 | + />, |
| 495 | + ); |
| 496 | + |
| 497 | + let triggerCount = 1; |
| 498 | + |
| 499 | + const pickupPlace = screen.getByTestId('pickup-zone'); |
| 500 | + let input = within(pickupPlace).getByLabelText(/pickup zone/i); |
| 501 | + pickupPlace.focus(); |
| 502 | + fireEvent.change(input, { target: { value: 'a' } }); |
| 503 | + fireEvent.keyDown(pickupPlace, { key: 'ArrowDown' }); |
| 504 | + fireEvent.keyDown(pickupPlace, { key: 'Enter' }); |
| 505 | + expect(onChange).toHaveBeenCalledTimes(triggerCount); |
| 506 | + expect(onValidate).toHaveBeenCalledTimes(triggerCount); |
| 507 | + triggerCount += 1; |
| 508 | + |
| 509 | + const cartId = screen.getByTestId('cart-id'); |
| 510 | + cartId.focus(); |
| 511 | + input = within(cartId).getByLabelText(/cart id/i); |
| 512 | + fireEvent.change(input, { target: { value: 'a' } }); |
| 513 | + fireEvent.keyDown(pickupPlace, { key: 'ArrowDown' }); |
| 514 | + fireEvent.keyDown(pickupPlace, { key: 'Enter' }); |
| 515 | + expect(onChange).toHaveBeenCalledTimes(triggerCount); |
| 516 | + expect(onValidate).toHaveBeenCalledTimes(triggerCount); |
| 517 | + triggerCount += 1; |
| 518 | + |
| 519 | + const dropoffPlace = screen.getByTestId('dropoff-location'); |
| 520 | + dropoffPlace.focus(); |
| 521 | + input = within(dropoffPlace).getByLabelText(/dropoff location/i); |
| 522 | + fireEvent.change(input, { target: { value: 'a' } }); |
| 523 | + fireEvent.keyDown(dropoffPlace, { key: 'ArrowDown' }); |
| 524 | + fireEvent.keyDown(dropoffPlace, { key: 'Enter' }); |
| 525 | + expect(onChange).toHaveBeenCalledTimes(triggerCount); |
| 526 | + expect(onValidate).toHaveBeenCalledTimes(triggerCount); |
| 527 | + triggerCount += 1; |
| 528 | + }); |
| 529 | + |
| 530 | + it('delivery custom booking label', () => { |
| 531 | + let desc = makeDefaultDeliveryCustomTaskDescription('delivery_sequential_lot_pickup'); |
| 532 | + desc = deliveryCustomInsertPickup(desc, 'test_place', 'test_lot'); |
| 533 | + desc = deliveryCustomInsertCartId(desc, 'test_cart_id'); |
| 534 | + desc = deliveryCustomInsertDropoff(desc, 'test_dropoff'); |
| 535 | + let label = makeDeliveryCustomTaskBookingLabel(desc); |
| 536 | + expect(label.task_definition_id).toBe( |
| 537 | + DeliverySequentialLotPickupTaskDefinition.taskDefinitionId, |
| 538 | + ); |
| 539 | + expect(label.pickup).toBe('test_lot'); |
| 540 | + expect(label.destination).toBe('test_dropoff'); |
| 541 | + expect(label.cart_id).toBe('test_cart_id'); |
| 542 | + |
| 543 | + desc = makeDefaultDeliveryCustomTaskDescription('delivery_area_pickup'); |
| 544 | + desc = deliveryCustomInsertPickup(desc, 'test_place', 'test_lot'); |
| 545 | + desc = deliveryCustomInsertCartId(desc, 'test_cart_id'); |
| 546 | + desc = deliveryCustomInsertDropoff(desc, 'test_dropoff'); |
| 547 | + label = makeDeliveryCustomTaskBookingLabel(desc); |
| 548 | + expect(label.task_definition_id).toBe(DeliveryAreaPickupTaskDefinition.taskDefinitionId); |
| 549 | + expect(label.pickup).toBe('test_lot'); |
| 550 | + expect(label.destination).toBe('test_dropoff'); |
| 551 | + expect(label.cart_id).toBe('test_cart_id'); |
| 552 | + }); |
| 553 | + |
| 554 | + it('delivery custom validity', () => { |
| 555 | + let desc = makeDefaultDeliveryCustomTaskDescription('delivery_sequential_lot_pickup'); |
| 556 | + expect( |
| 557 | + isDeliveryCustomTaskDescriptionValid( |
| 558 | + desc, |
| 559 | + Object.values(mockPickupZones), |
| 560 | + Object.keys(mockDropoffPoints), |
| 561 | + ), |
| 562 | + ).not.toBeTruthy(); |
| 563 | + desc = deliveryCustomInsertPickup(desc, 'pickup_1', 'zone_1'); |
| 564 | + desc = deliveryCustomInsertCartId(desc, 'cart_1'); |
| 565 | + desc = deliveryCustomInsertDropoff(desc, 'dropoff_1'); |
| 566 | + expect( |
| 567 | + isDeliveryCustomTaskDescriptionValid( |
| 568 | + desc, |
| 569 | + Object.values(mockPickupZones), |
| 570 | + Object.keys(mockDropoffPoints), |
| 571 | + ), |
| 572 | + ).toBeTruthy(); |
| 573 | + }); |
| 574 | + |
| 575 | + it('delivery custom short description', () => { |
| 576 | + let desc = makeDefaultDeliveryCustomTaskDescription('delivery_sequential_lot_pickup'); |
| 577 | + desc = deliveryCustomInsertPickup(desc, 'pickup_1', 'zone_1'); |
| 578 | + desc = deliveryCustomInsertCartId(desc, 'cart_1'); |
| 579 | + desc = deliveryCustomInsertDropoff(desc, 'dropoff_1'); |
| 580 | + expect(makeDeliveryCustomTaskShortDescription(desc, undefined)).toBe( |
| 581 | + '[Delivery - Sequential lot pick up] payload [cart_1] from [pickup_1] to [dropoff_1]', |
| 582 | + ); |
| 583 | + }); |
372 | 584 | });
|
0 commit comments