Skip to content

Commit

Permalink
refactor: move mock api pool to test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandenaardweg committed Dec 19, 2022
1 parent 2ac95d1 commit 6431388
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 102 deletions.
21 changes: 1 addition & 20 deletions src/energy-socket-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
import { Interceptable, MockAgent, setGlobalDispatcher } from 'undici';
import { mockBasicInformationResponse } from '@/mocks/data/basic';
import { mockStateResponse } from '@/mocks/data/state';
import { mockIdentifyResponse } from '@/mocks/data/identify';
import { mockApiUrl } from '@/mocks/api';
import { mockApiPool, mockApiUrl } from '@/mocks/api';
import { StateResponse, SystemPutParams } from '@/types';
import { mockEnergySocketDataResponse } from '@/mocks/data/data';
import { mockSystemResponse } from '@/mocks/data/system';
import { EnergySocketApi } from '@/energy-socket-api';

let mockApiAgent: MockAgent;
let mockApiPool: Interceptable;
let energySocketApi: EnergySocketApi;

const mockBasicResponse = mockBasicInformationResponse['HWE-SKT'];

describe('EnergySocketApi', () => {
beforeEach(() => {
energySocketApi = new EnergySocketApi(mockApiUrl);

mockApiAgent = new MockAgent({
bodyTimeout: 10,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
});

mockApiAgent.disableNetConnect();

setGlobalDispatcher(mockApiAgent);

mockApiPool = mockApiAgent.get(mockApiUrl);
});

afterEach(async () => {
await mockApiAgent.close();
});

it('should be able to create a new instance', () => {
Expand Down
21 changes: 1 addition & 20 deletions src/kwh-meter-1-phase-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import { mockBasicInformationResponse } from '@/mocks/data/basic';

import { mockApiUrl } from '@/mocks/api';
import { Interceptable, MockAgent, setGlobalDispatcher } from 'undici';
import { mockApiPool, mockApiUrl } from '@/mocks/api';
import { mockKwhMeter1PhaseResponse } from '@/mocks/data/data';
import { KwhMeter1PhaseApi } from '@/kwh-meter-1-phase-api';

let mockApiAgent: MockAgent;
let mockApiPool: Interceptable;
let kwhMeter1PhaseApi: KwhMeter1PhaseApi;

const mockBasicResponse = mockBasicInformationResponse['SDM230-wifi'];

describe('KwhMeter1PhaseApi', () => {
beforeEach(() => {
kwhMeter1PhaseApi = new KwhMeter1PhaseApi(mockApiUrl);

mockApiAgent = new MockAgent({
bodyTimeout: 10,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
});

mockApiAgent.disableNetConnect();

setGlobalDispatcher(mockApiAgent);

mockApiPool = mockApiAgent.get(mockApiUrl);
});

afterEach(async () => {
await mockApiAgent.close();
});

it('should be able to create a new instance', () => {
Expand Down
21 changes: 1 addition & 20 deletions src/kwh-meter-3-phase-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import { mockBasicInformationResponse } from '@/mocks/data/basic';

import { mockApiUrl } from '@/mocks/api';
import { Interceptable, MockAgent, setGlobalDispatcher } from 'undici';
import { mockApiPool, mockApiUrl } from '@/mocks/api';
import { mockKwhMeter3PhaseResponse } from '@/mocks/data/data';
import { KwhMeter3PhaseApi } from '@/kwh-meter-3-phase-api';

let mockApiAgent: MockAgent;
let mockApiPool: Interceptable;
let kwhMeter3PhaseApi: KwhMeter3PhaseApi;

const mockBasicResponse = mockBasicInformationResponse['SDM630-wifi'];

describe('WaterMeterApi', () => {
beforeEach(() => {
kwhMeter3PhaseApi = new KwhMeter3PhaseApi(mockApiUrl);

mockApiAgent = new MockAgent({
bodyTimeout: 10,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
});

mockApiAgent.disableNetConnect();

setGlobalDispatcher(mockApiAgent);

mockApiPool = mockApiAgent.get(mockApiUrl);
});

afterEach(async () => {
await mockApiAgent.close();
});

it('should be able to create a new instance', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/mocks/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MockAgent } from 'undici';
import { SupportedDevices } from '../types';

export const mockHostname = 'localhost';
Expand All @@ -7,3 +8,13 @@ export const mockFirmwareVersion = '3.1';
export const mockProductType: SupportedDevices = 'HWE-SKT';
export const mockProductName = 'Energy Socket';
export const mockApiVersion = 'v1';

export const mockApiAgent = new MockAgent({
bodyTimeout: 10,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
});

mockApiAgent.disableNetConnect();

export const mockApiPool = mockApiAgent.get(mockApiUrl);
21 changes: 1 addition & 20 deletions src/p1-meter-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { mockBasicInformationResponse } from '@/mocks/data/basic';

import { mockApiUrl } from '@/mocks/api';
import { Interceptable, MockAgent, setGlobalDispatcher } from 'undici';
import { mockApiPool, mockApiUrl } from '@/mocks/api';
import { mockP1MeterDataResponse } from '@/mocks/data/data';
import { mockParsedTelegramResponse, mockTelegramResponse } from '@/mocks/data/telegram';
import { P1MeterApi } from '@/p1-meter-api';

let mockApiAgent: MockAgent;
let mockApiPool: Interceptable;
let p1MeterApi: P1MeterApi;

const mockBasicResponse = mockBasicInformationResponse['HWE-P1'];
Expand All @@ -19,22 +16,6 @@ describe('HomeWizardEnergyApi', () => {
interval: 10,
},
});

mockApiAgent = new MockAgent({
bodyTimeout: 10,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
});

mockApiAgent.disableNetConnect();

setGlobalDispatcher(mockApiAgent);

mockApiPool = mockApiAgent.get(mockApiUrl);
});

afterEach(async () => {
await mockApiAgent.close();
});

describe('constructor', () => {
Expand Down
21 changes: 1 addition & 20 deletions src/water-meter-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import { mockBasicInformationResponse } from '@/mocks/data/basic';

import { mockApiUrl } from '@/mocks/api';
import { Interceptable, MockAgent, setGlobalDispatcher } from 'undici';
import { mockApiPool, mockApiUrl } from '@/mocks/api';
import { mockWaterMeterDataResponse } from '@/mocks/data/data';
import { WaterMeterApi } from '@/water-meter-api';

let mockApiAgent: MockAgent;
let mockApiPool: Interceptable;
let waterMeterApi: WaterMeterApi;

const mockBasicResponse = mockBasicInformationResponse['HWE-WTR'];

describe('WaterMeterApi', () => {
beforeEach(() => {
waterMeterApi = new WaterMeterApi(mockApiUrl);

mockApiAgent = new MockAgent({
bodyTimeout: 10,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
});

mockApiAgent.disableNetConnect();

setGlobalDispatcher(mockApiAgent);

mockApiPool = mockApiAgent.get(mockApiUrl);
});

afterEach(async () => {
await mockApiAgent.close();
});

it('should be able to create a new instance', () => {
Expand Down
14 changes: 12 additions & 2 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { afterEach, beforeEach, vi } from 'vitest';
import { setGlobalDispatcher } from 'undici';
import { afterAll, afterEach, beforeAll, beforeEach, vi } from 'vitest';
import { mockApiAgent } from './src/mocks/api';

beforeAll(() => {
setGlobalDispatcher(mockApiAgent);
});

beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
afterEach(async () => {
vi.clearAllMocks();
vi.clearAllTimers();
vi.runAllTimers();
vi.useRealTimers();
});

afterAll(async () => {
await mockApiAgent.close();
});

0 comments on commit 6431388

Please sign in to comment.