refactor: improve async handling

This commit is contained in:
Carlos Valente
2025-02-15 14:05:53 +01:00
committed by Carlos Valente
parent c65761448d
commit 891bda6661
8 changed files with 88 additions and 87 deletions
@@ -40,43 +40,43 @@ afterAll(() => {
});
describe('addTrigger()', () => {
beforeEach(() => {
deleteAllTriggers();
beforeEach(async () => {
await deleteAllTriggers();
});
it('should accept a valid automation', () => {
it('should accept a valid trigger', async () => {
const testData: TriggerDTO = {
title: 'test',
trigger: TimerLifeCycle.onLoad,
automationId: 'test-automation-id',
};
const trigger = addTrigger(testData);
const trigger = await addTrigger(testData);
expect(trigger).toMatchObject(testData);
});
});
describe('editTrigger()', () => {
beforeEach(() => {
deleteAllTriggers();
addTrigger({
beforeEach(async () => {
await deleteAllTriggers();
await addTrigger({
title: 'test-osc',
trigger: TimerLifeCycle.onLoad,
automationId: 'test-osc-automation',
});
addTrigger({
await addTrigger({
title: 'test-http',
trigger: TimerLifeCycle.onFinish,
automationId: 'test-http-automation',
});
});
it('should edit the contents of an automation', () => {
it('should edit the contents of a trigger', async () => {
const triggers = getAutomationTriggers();
const fistTrigger = triggers[0];
expect(fistTrigger).toMatchObject({ id: expect.any(String), title: 'test-osc' });
const editedOSC = editTrigger(fistTrigger.id, {
const editedOSC = await editTrigger(fistTrigger.id, {
title: 'edited-title',
trigger: TimerLifeCycle.onDanger,
automationId: 'test-osc-automation',
@@ -92,9 +92,9 @@ describe('editTrigger()', () => {
});
describe('deleteTrigger()', () => {
beforeEach(() => {
deleteAllTriggers();
addTrigger({
beforeEach(async () => {
await deleteAllTriggers();
await addTrigger({
title: 'test-osc',
trigger: TimerLifeCycle.onLoad,
automationId: 'test-osc-automation',
@@ -106,13 +106,13 @@ describe('deleteTrigger()', () => {
});
});
it('should remove an automation from the list', () => {
it('should remove an automation from the list', async () => {
const triggers = getAutomationTriggers();
expect(triggers.length).toEqual(2);
const fistTrigger = triggers[0];
expect(fistTrigger).toMatchObject({ id: expect.any(String), title: 'test-osc' });
deleteTrigger(fistTrigger.id);
await deleteTrigger(fistTrigger.id);
const removed = getAutomationTriggers();
expect(removed.length).toEqual(1);
expect(removed[0].title).not.toEqual('test-osc');
@@ -120,11 +120,11 @@ describe('deleteTrigger()', () => {
});
describe('addAutomation()', () => {
beforeEach(() => {
deleteAll();
beforeEach(async () => {
await deleteAll();
});
it('should accept a valid automation', () => {
it('should accept a valid automation', async () => {
const testData: AutomationDTO = {
title: 'test',
filterRule: 'all',
@@ -132,24 +132,24 @@ describe('addAutomation()', () => {
outputs: [makeOSCAction(), makeHTTPAction()],
};
const automation = addAutomation(testData);
const automation = await addAutomation(testData);
const automations = getAutomations();
expect(automations[automation.id]).toMatchObject(testData);
});
});
describe('editAutomation()', () => {
describe('editAutomation()', async () => {
// saving the ID of the added automation
let firstAutomation: Automation;
beforeEach(() => {
deleteAll();
firstAutomation = addAutomation({
beforeEach(async () => {
await deleteAll();
firstAutomation = await addAutomation({
title: 'test-osc',
filterRule: 'all',
filters: [],
outputs: [],
});
addAutomation({
await addAutomation({
title: 'test-http',
filterRule: 'all',
filters: [],
@@ -157,7 +157,7 @@ describe('editAutomation()', () => {
});
});
it('should edit the contents of an automation', () => {
it('should edit the contents of an automation', async () => {
const automations = getAutomations();
expect(Object.keys(automations).length).toEqual(2);
expect(automations[firstAutomation.id]).toMatchObject({
@@ -168,7 +168,7 @@ describe('editAutomation()', () => {
outputs: expect.any(Array),
});
const editedOSC = editAutomation(firstAutomation.id, {
const editedOSC = await editAutomation(firstAutomation.id, {
title: 'edited-title',
filterRule: 'any',
filters: [],
@@ -188,9 +188,9 @@ describe('editAutomation()', () => {
describe('deleteAutomation()', () => {
// saving the ID of the added automation
let firstAutomation: Automation;
beforeEach(() => {
deleteAll();
firstAutomation = addAutomation({
beforeEach(async () => {
await deleteAll();
firstAutomation = await addAutomation({
title: 'test-osc',
filterRule: 'all',
filters: [],
@@ -198,18 +198,18 @@ describe('deleteAutomation()', () => {
});
});
it('should remove m automation from the list', () => {
it('should remove m automation from the list', async () => {
const automations = getAutomations();
expect(Object.keys(automations).length).toEqual(1);
deleteAutomation(Object.keys(automations)[0]);
await deleteAutomation(Object.keys(automations)[0]);
const removed = getAutomations();
expect(Object.keys(removed).length).toEqual(0);
});
it('should not remove an automation which is in use', () => {
it('should not remove an automation which is in use', async () => {
const automations = getAutomations();
addTrigger({
await addTrigger({
title: 'test-automation',
trigger: TimerLifeCycle.onLoad,
automationId: firstAutomation.id,
@@ -227,6 +227,6 @@ describe('deleteAutomation()', () => {
outputs: expect.any(Array),
});
expect(() => deleteAutomation(automationId)).toThrowError();
await expect(deleteAutomation(automationId)).rejects.toThrowError();
});
});
@@ -39,29 +39,29 @@ describe('triggerAction()', () => {
let oscSpy = vi.spyOn(oscClient, 'emitOSC');
let httpSpy = vi.spyOn(httpClient, 'emitHTTP');
beforeEach(() => {
beforeEach(async () => {
oscSpy = vi.spyOn(oscClient, 'emitOSC').mockImplementation(() => {});
httpSpy = vi.spyOn(httpClient, 'emitHTTP').mockImplementation(() => {});
deleteAllTriggers();
const oscAutomation = addAutomation({
await deleteAllTriggers();
const oscAutomation = await addAutomation({
title: 'test-osc',
filterRule: 'all',
filters: [],
outputs: [makeOSCAction()],
});
const httpAutomation = addAutomation({
const httpAutomation = await addAutomation({
title: 'test-http',
filterRule: 'any',
filters: [],
outputs: [makeHTTPAction()],
});
addTrigger({
await addTrigger({
title: 'test-osc',
trigger: TimerLifeCycle.onLoad,
automationId: oscAutomation.id,
});
addTrigger({
await addTrigger({
title: 'test-http',
trigger: TimerLifeCycle.onFinish,
automationId: httpAutomation.id,