Skip to content

Commit 5309e1f

Browse files
author
Christian
committed
Improve the testability of MessageStore
This changeintroduces a **targeted** approach to improve the testability of MessageStore: - Created a new ConfigurationStore as a Pinia store to replace usage of useConfiguration composable in MessageStore - Created a new EditRetryStore as a Pinia store to replace useEditAndRetry composable usage in MessageStore - Removed automatic initialization in the store definition to avoid network calls during tests and take advantage of Pinia testing infraestructure that stubs actions by default and allow tests to patch the stores state instead for each scenario - The Message store explicitly calls loadConfig() of the new stores during initialization, which in turn is properly stubbed by Pinia when used in tests - This allows component tests to run without unwanted network calls when patch the store state is preffered by the developer writing the component test - The original useConfiguration composable remains untouched for other components This focused refactoring prevents network calls from being made during tests while minimizing changes to the codebase, as it only affects MessageStore.ts without requiring a broader refactoring across all components that use configuration data.
1 parent 648785a commit 5309e1f

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

src/Frontend/src/composables/useEditAndRetry.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { acceptHMRUpdate, defineStore } from "pinia";
2+
import { ref } from "vue";
3+
import Configuration from "@/resources/Configuration";
4+
import { useFetchFromServiceControl } from "@/composables/serviceServiceControlUrls";
5+
6+
export const useConfigurationStore = defineStore("ConfigurationStore", () => {
7+
const configuration = ref<Configuration | null>(null);
8+
9+
async function loadConfig() {
10+
const response = await useFetchFromServiceControl("configuration");
11+
configuration.value = await response.json();
12+
}
13+
14+
return {
15+
configuration,
16+
loadConfig,
17+
};
18+
});
19+
20+
if (import.meta.hot) {
21+
import.meta.hot.accept(acceptHMRUpdate(useConfigurationStore, import.meta.hot));
22+
}
23+
24+
export type ConfigurationStore = ReturnType<typeof useConfigurationStore>;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { acceptHMRUpdate, defineStore } from "pinia";
2+
import { ref } from "vue";
3+
import { EditAndRetryConfig } from "@/resources/Configuration";
4+
import { useTypedFetchFromServiceControl } from "@/composables/serviceServiceControlUrls";
5+
6+
export const useEditRetryStore = defineStore("EditRetryStore", () => {
7+
const config = ref<EditAndRetryConfig>({ enabled: false, locked_headers: [], sensitive_headers: [] });
8+
9+
async function loadConfig() {
10+
const [, data] = await useTypedFetchFromServiceControl<EditAndRetryConfig>("edit/config");
11+
config.value = data;
12+
}
13+
14+
return {
15+
config,
16+
loadConfig,
17+
};
18+
});
19+
20+
if (import.meta.hot) {
21+
import.meta.hot.accept(acceptHMRUpdate(useEditRetryStore, import.meta.hot));
22+
}
23+
24+
export type EditRetryStore = ReturnType<typeof useEditRetryStore>;

src/Frontend/src/stores/MessageStore.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { reactive, ref } from "vue";
33
import Header from "@/resources/Header";
44
import type EndpointDetails from "@/resources/EndpointDetails";
55
import FailedMessage, { ExceptionDetails, FailedMessageStatus } from "@/resources/FailedMessage";
6-
import { editRetryConfig } from "@/composables/useEditAndRetry";
6+
import { useEditRetryStore } from "@/stores/EditRetryStore";
7+
import { useConfigurationStore } from "@/stores/ConfigurationStore";
78
import { useFetchFromServiceControl, useTypedFetchFromServiceControl } from "@/composables/serviceServiceControlUrls";
89
import Message, { MessageStatus } from "@/resources/Message";
910
import moment from "moment/moment";
10-
import { useConfiguration } from "@/composables/configuration";
1111
import { parse, stringify } from "lossless-json";
1212
import xmlFormat from "xml-formatter";
1313
import { useArchiveMessage, useRetryMessages, useUnarchiveMessage } from "@/composables/serviceFailedMessage";
@@ -67,6 +67,11 @@ export const useMessageStore = defineStore("MessageStore", () => {
6767
let bodyLoadedId = "";
6868
let conversationLoadedId = "";
6969
const conversationData = ref<DataContainer<Message[]>>({ data: [] });
70+
const editRetryStore = useEditRetryStore();
71+
const configStore = useConfigurationStore();
72+
73+
editRetryStore.loadConfig();
74+
configStore.loadConfig();
7075

7176
function reset() {
7277
state.data = { failure_metadata: {}, failure_status: {}, dialog_status: {} };
@@ -284,14 +289,13 @@ export const useMessageStore = defineStore("MessageStore", () => {
284289
return exportString;
285290
}
286291

287-
const configuration = useConfiguration();
288-
const error_retention_period = moment.duration(configuration.value?.data_retention.error_retention_period).asHours();
292+
const error_retention_period = moment.duration(configStore.configuration?.data_retention?.error_retention_period).asHours();
289293

290294
return {
291295
headers,
292296
body,
293297
state,
294-
edit_and_retry_config: editRetryConfig,
298+
edit_and_retry_config: editRetryStore.config,
295299
reset,
296300
loadMessage,
297301
loadFailedMessage,

0 commit comments

Comments
 (0)