UNPKG

@telstra/iot-connectivity-manager

Version:
79 lines (72 loc) 2.97 kB
import { vi } from 'vitest'; import { AssertionError } from '@telstra/core'; import { Services } from '../../../src/classes/Services.js'; import { Constants } from '../../shared/Constants.js'; import AUTH_CONFIG from '../../shared/credentials.json' with { type: 'json' }; import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest'; import { server } from '../../shared/mswServer.js'; describe('Services', () => { const services = new Services(AUTH_CONFIG); beforeAll(() => { server.listen(); server.resetHandlers(); }); afterEach(() => { server.resetHandlers(); vi.clearAllMocks(); }); afterAll(() => server.close()); describe('validateIMSIParam', () => { it('should not throw for valid IMSI', () => { expect(() => services.validateIMSIParam('123456789012345')).not.toThrow(); }); it('should throw for invalid IMSI (too short)', () => { expect(() => services.validateIMSIParam('123')).toThrow(AssertionError); }); it('should throw for invalid IMSI (non-numeric)', () => { expect(() => services.validateIMSIParam('12345678901234a')).toThrow(AssertionError); }); it('should throw for invalid IMSI (too long)', () => { expect(() => services.validateIMSIParam('1234567890123456')).toThrow(AssertionError); }); }); describe('getAll', () => { describe('when the client sends a valid request', async () => { it('should pass', async () => { await expect(services.getAll()).resolves.toEqual([]); }); }); }); describe('getByImsi', () => { describe('when the client sends a valid request', async () => { it('should pass', async () => { await expect(services.getByImsi('123456789012345')).resolves.toEqual({}); }); }); describe('when the client sends an invalid request', () => { it('should fail', async () => { const callback = async () => services.getByImsi('123456'); await expect(callback).rejects.toThrow(AssertionError); }); }); describe('when the backend returns 404 with ERR_BAD_REQUEST', () => { it('should throw ICMServiceUnavailable AssertionError', async () => { // Mock the instance.get to throw a 404 + ERR_BAD_REQUEST error services.instance.get = vi.fn().mockRejectedValue({ response: { status: 404 }, code: 'ERR_BAD_REQUEST', }); const callback = async () => services.getByImsi('123456789012345'); await expect(callback).rejects.toThrow(AssertionError); }); }); describe('when the backend returns an unexpected error', () => { it('should rethrow the error', async () => { const error = new Error('Unexpected error'); services.instance.get = vi.fn().mockRejectedValue(error); const callback = async () => services.getByImsi('123456789012345'); await expect(callback).rejects.toThrow('Unexpected error'); }); }); }); });