|
| 1 | +// @vitest-environment happy-dom |
| 2 | + |
| 3 | +import { type Mock, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 4 | +import { NavigationServiceClient } from '../../gen/service/navigation/v1/navigation_pb_service'; |
| 5 | +vi.mock('../../gen/service/navigation/v1/navigation_pb_service'); |
| 6 | +import { RobotClient } from '../../robot'; |
| 7 | +vi.mock('../../robot'); |
| 8 | + |
| 9 | +import { NavigationClient } from './client'; |
| 10 | + |
| 11 | +const navigationClientName = 'test-navigation'; |
| 12 | + |
| 13 | +let navigation: NavigationClient; |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + RobotClient.prototype.createServiceClient = vi |
| 17 | + .fn() |
| 18 | + .mockImplementation( |
| 19 | + () => new NavigationServiceClient(navigationClientName) |
| 20 | + ); |
| 21 | + |
| 22 | + navigation = new NavigationClient( |
| 23 | + new RobotClient('host'), |
| 24 | + navigationClientName |
| 25 | + ); |
| 26 | +}); |
| 27 | + |
| 28 | +const testLatitude = 50; |
| 29 | +const testLongitude = 75; |
| 30 | +const testCompassHeading = 90; |
| 31 | + |
| 32 | +describe('getLocation', () => { |
| 33 | + let latitude: Mock<[], number>; |
| 34 | + let longitude: Mock<[], number>; |
| 35 | + let compassHeading: Mock<[], number>; |
| 36 | + let location: Mock<[], { latitude: number; longitude: number }>; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + location = vi.fn(() => ({ |
| 40 | + latitude: latitude(), |
| 41 | + longitude: longitude(), |
| 42 | + })); |
| 43 | + |
| 44 | + NavigationServiceClient.prototype.getLocation = vi |
| 45 | + .fn() |
| 46 | + .mockImplementation((_req, _md, cb) => { |
| 47 | + cb(null, { |
| 48 | + toObject: () => ({ |
| 49 | + compassHeading: compassHeading(), |
| 50 | + location: location(), |
| 51 | + }), |
| 52 | + }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + test('null location', async () => { |
| 57 | + location = vi.fn(); |
| 58 | + compassHeading = vi.fn(); |
| 59 | + |
| 60 | + await expect(navigation.getLocation()).rejects.toThrowError( |
| 61 | + /^no location$/u |
| 62 | + ); |
| 63 | + |
| 64 | + expect(location).toHaveBeenCalledOnce(); |
| 65 | + expect(compassHeading).toHaveBeenCalledOnce(); |
| 66 | + }); |
| 67 | + |
| 68 | + test('valid geopoint', async () => { |
| 69 | + latitude = vi.fn(() => testLatitude); |
| 70 | + longitude = vi.fn(() => testLongitude); |
| 71 | + compassHeading = vi.fn(() => testCompassHeading); |
| 72 | + |
| 73 | + const expected = { |
| 74 | + location: { latitude: testLatitude, longitude: testLongitude }, |
| 75 | + compassHeading: testCompassHeading, |
| 76 | + }; |
| 77 | + |
| 78 | + await expect(navigation.getLocation()).resolves.toStrictEqual(expected); |
| 79 | + |
| 80 | + expect(location).toHaveBeenCalledOnce(); |
| 81 | + expect(compassHeading).toHaveBeenCalledOnce(); |
| 82 | + }); |
| 83 | + |
| 84 | + test('invalid geopoint', async () => { |
| 85 | + latitude = vi.fn(() => Number.NaN); |
| 86 | + longitude = vi.fn(() => Number.NaN); |
| 87 | + |
| 88 | + await expect(navigation.getLocation()).rejects.toThrowError( |
| 89 | + /^invalid location$/u |
| 90 | + ); |
| 91 | + |
| 92 | + expect(location).toHaveBeenCalledOnce(); |
| 93 | + expect(compassHeading).toHaveBeenCalledOnce(); |
| 94 | + }); |
| 95 | +}); |
0 commit comments