Files
doormilxpress_astryx/tests/lib/zoneHubIds.test.jsx

181 lines
7.8 KiB
JavaScript

import { renderHook, act } from '@testing-library/react';
jest.mock('@/lib/doormileHooks', () => ({
__esModule: true,
useHubs: jest.fn(),
useTenantLocations: jest.fn()
}));
jest.mock('@/lib/AuthContext', () => ({ __esModule: true, useAuth: jest.fn() }));
import { ZoneProvider, useZone } from '@/lib/ZoneContext';
import { useHubs, useTenantLocations } from '@/lib/doormileHooks';
import { useAuth } from '@/lib/AuthContext';
/**
* Which orders belong to the selected zone.
*
* These fixtures are the shapes the API really returns, not convenient ones.
* `GET /admin/hubs` serves models.Hub, and `GET /admin/bookings` serves
* models.PickupBooking — so a hub here carries `applocationid` and `city`, and
* a booking carries `pickuphubid` and `nearesthubid` and NOT `hubid`,
* `sourcehubid` or `servicinghubid`. The older suite in ZoneContext.test.jsx
* uses a hub fixture with a `city` the endpoint did not send, which is how a
* green suite sat on top of a city comparison that could never run.
*/
// Hub 5, Bengaluru. `city` is populated by attachHubCities in adminController.
const BLR_EARTH = {
hubid: 5, hubname: 'Bangalore Earth Hub', hubtype: 'sorting_center',
applocationid: 3, city: 'Bangalore', address: '', latitude: 12.9352,
longitude: 77.6244, pincode: '560034', status: 'Active'
};
const CBE_JUPITER = {
hubid: 1, hubname: 'Coimbatore Jupiter Hub', hubtype: 'sorting_center',
applocationid: 1, city: 'Coimbatore', address: '', latitude: 11.0168,
longitude: 76.9558, pincode: '641012', status: 'Active'
};
const wrapper = ({ children }) => <ZoneProvider>{children}</ZoneProvider>;
const setup = ({ user = { tenantid: null }, hubs = [CBE_JUPITER, BLR_EARTH], locations = [] } = {}) => {
useAuth.mockReturnValue({ user });
useHubs.mockReturnValue({ data: hubs, isLoading: false, refetch: jest.fn() });
useTenantLocations.mockReturnValue({ data: locations, isLoading: false, refetch: jest.fn() });
return renderHook(() => useZone(), { wrapper });
};
const inZone = (result, zoneId, item) => {
act(() => result.current.setSelectedZoneId(zoneId));
return result.current.matchesZone(item);
};
beforeEach(() => localStorage.clear());
describe('a booking is matched by the hub it actually records', () => {
it('pickuphubid alone puts the order in that zone', () => {
// The column exists, is populated by createExpressBooking, and was being
// ignored. Nothing else on this row can place it: no coordinates, and an
// address that names neither the hub nor its city.
const { result } = setup();
expect(inZone(result, '5', {
bookingid: 1, bookingno: 'DM-BLR001', pickuphubid: 5,
pickupaddress: 'Gate 3', deliveryaddress: 'Block C'
})).toBe(true);
});
it('nearesthubid alone puts the order in that zone', () => {
const { result } = setup();
expect(inZone(result, '5', {
bookingid: 2, bookingno: 'DM-BLR002', nearesthubid: 5,
pickupaddress: 'Gate 3', deliveryaddress: 'Block C'
})).toBe(true);
});
it('and does NOT put it in a different hub\'s zone', () => {
const { result } = setup();
expect(inZone(result, '1', {
bookingid: 1, bookingno: 'DM-BLR001', pickuphubid: 5,
pickupaddress: 'Gate 3', deliveryaddress: 'Block C'
})).toBe(false);
});
});
describe('a tenant session never matches on a hub id', () => {
const KITCHEN = { tenantlocationid: 5, locationname: 'Indiranagar Kitchen', city: 'Bengaluru',
latitude: 12.9784, longitude: 77.6408, pincode: '560038' };
it('a booking whose pickuphubid is 5 does not land in tenant location 5', () => {
// Two different id spaces. Matching them would file an order under a
// kitchen it has nothing to do with, on a bare numeric collision.
const { result } = setup({ user: { tenantid: '13' }, locations: [KITCHEN] });
expect(inZone(result, '5', {
bookingid: 3, bookingno: 'DM-X', tenantid: '13', pickuphubid: 5,
pickupaddress: 'Gate 3', deliveryaddress: 'Block C'
})).toBe(false);
});
it('but its own tenantlocationid still matches', () => {
const { result } = setup({ user: { tenantid: '13' }, locations: [KITCHEN] });
expect(inZone(result, '5', {
bookingid: 4, bookingno: 'DM-Y', tenantid: '13', tenantlocationid: 5,
pickupaddress: 'Gate 3', deliveryaddress: 'Block C'
})).toBe(true);
});
});
describe('the city comparison, which needs a city on the hub', () => {
it('matches a Bangalore address against the Bangalore hub', () => {
// `hub.city` comes from the applocations join. Before that field existed
// this branch read '' and was skipped on every staff session, leaving a
// 35km radius as the only thing still matching.
const { result } = setup();
expect(inZone(result, '5', {
bookingid: 5, bookingno: 'DM-BLR003',
pickupaddress: '80 Feet Road, Koramangala, Bangalore',
deliveryaddress: '100 Feet Road, Indiranagar, Bangalore'
})).toBe(true);
});
it('a hub with no city does not match everything', () => {
// Failing open here would put every order in every zone.
const { result } = setup({ hubs: [{ ...BLR_EARTH, city: undefined, latitude: undefined, longitude: undefined }] });
expect(inZone(result, '5', {
bookingid: 6, bookingno: 'DM-Z',
pickupaddress: 'Somewhere else entirely', deliveryaddress: 'Also elsewhere'
})).toBe(false);
});
});
describe('an order with no coordinates is not silently lost', () => {
it('still reaches its zone through pickuphubid', () => {
// The case that started this: coordinates were the only working matcher,
// so an order saved without them was invisible in every zone at once.
const { result } = setup();
const noCoords = {
bookingid: 7, bookingno: 'DM-NOCOORD', pickuphubid: 5,
pickuplatitude: 0, pickuplongitude: 0,
deliverylatitude: 0, deliverylongitude: 0,
pickupaddress: 'Gate 3', deliveryaddress: 'Block C'
};
expect(inZone(result, '5', noCoords)).toBe(true);
expect(inZone(result, '1', noCoords)).toBe(false);
});
});
describe('one id space at a time', () => {
// queries.js puts servicinghubid, applocationid and tenantlocationid on the
// SAME delivery row. They are three different numbering schemes, and testing
// them all against a hub id files orders under cities they never touched.
const DM626241 = {
bookingid: 11, bookingno: 'DM-626241',
servicinghubid: 5, // Bangalore Earth Hub — the real owner
tenantlocationid: 3, // a client site; as a HUB id this is Hyderabad
applocationid: 3, // a CITY id; as a hub id this is Hyderabad too
pickupaddress: 'Koramangala', deliveryaddress: 'Mysore Road',
};
const HYDERABAD_MARS = {
hubid: 3, hubname: 'Hyderabad Mars Hub', applocationid: 2, city: 'Hyderabad',
latitude: 17.4483, longitude: 78.3741, pincode: '500032', status: 'Active',
};
it('a Bangalore run does not land in Hyderabad on a numeric collision', () => {
const { result } = setup({ hubs: [CBE_JUPITER, BLR_EARTH, HYDERABAD_MARS] });
expect(inZone(result, '3', DM626241)).toBe(false);
});
it('and still lands in Bangalore, by the hub it actually names', () => {
const { result } = setup({ hubs: [CBE_JUPITER, BLR_EARTH, HYDERABAD_MARS] });
expect(inZone(result, '5', DM626241)).toBe(true);
});
it('a tenant session matches its own locations and no hub id', () => {
const KITCHEN = { tenantlocationid: 5, locationname: 'Indiranagar Kitchen',
city: 'Bengaluru', latitude: 12.9784, longitude: 77.6408, pincode: '560038' };
const { result } = setup({ user: { tenantid: '13' }, locations: [KITCHEN] });
expect(inZone(result, '5', { bookingid: 1, tenantid: '13', tenantlocationid: 5,
pickupaddress: 'a', deliveryaddress: 'b' })).toBe(true);
expect(inZone(result, '5', { bookingid: 2, tenantid: '13', servicinghubid: 5,
pickupaddress: 'a', deliveryaddress: 'b' })).toBe(false);
});
});