Skip to content

Commit 90d1e38

Browse files
committed
test: improve test coverage for plantUMLSlice
1 parent d726752 commit 90d1e38

1 file changed

Lines changed: 42 additions & 10 deletions

File tree

‎packages/frontend/test/unit/store/plantUMLSlice.test.ts‎

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import plantUMLReducer, {
77

88
global.fetch = jest.fn();
99

10+
const hashDiagram = (diagram: string): string => {
11+
return Math.abs(
12+
Array.from(diagram).reduce((hash, char) => {
13+
const nextHash = (hash << 5) - hash + char.charCodeAt(0);
14+
return nextHash & nextHash;
15+
}, 0),
16+
).toString(36);
17+
};
18+
1019
const createTestStore = (initialState = {}) => {
1120
return configureStore({
1221
reducer: {
@@ -57,15 +66,7 @@ describe('plantUMLSlice', () => {
5766
it('should return cached SVG if available', async () => {
5867
const diagram = '@startuml\nA --> B\n@enduml';
5968
const cachedSvg = '<svg>cached</svg>';
60-
61-
// Calculate the actual hash for the diagram
62-
let hash = 0;
63-
for (let i = 0; i < diagram.length; i++) {
64-
const char = diagram.charCodeAt(i);
65-
hash = ((hash << 5) - hash) + char;
66-
hash = hash & hash;
67-
}
68-
const expectedHash = Math.abs(hash).toString(36);
69+
const expectedHash = hashDiagram(diagram);
6970

7071
const store = createTestStore({
7172
svgCache: { [expectedHash]: cachedSvg },
@@ -80,6 +81,37 @@ describe('plantUMLSlice', () => {
8081
});
8182
});
8283

84+
it('should fetch SVG successfully and cache it', async () => {
85+
const diagram = '@startuml\nA --> B\n@enduml';
86+
const svg = '<svg>generated</svg>';
87+
const expectedHash = hashDiagram(diagram);
88+
89+
(global.fetch as jest.Mock).mockResolvedValueOnce({
90+
ok: true,
91+
text: jest.fn().mockResolvedValue(svg),
92+
});
93+
94+
const store = createTestStore();
95+
const result = await store.dispatch(generatePlantUMLSvg(diagram));
96+
const state = store.getState().plantUML;
97+
98+
expect(global.fetch).toHaveBeenCalledWith('/api/plantuml/svg', {
99+
method: 'POST',
100+
headers: {
101+
'Content-Type': 'application/json',
102+
},
103+
body: JSON.stringify({ diagram }),
104+
});
105+
expect(result.type).toBe('plantUML/generateSvg/fulfilled');
106+
expect(result.payload).toEqual({
107+
hash: expectedHash,
108+
svg,
109+
});
110+
expect(state.svgCache[expectedHash]).toBe(svg);
111+
expect(state.loading[expectedHash]).toBe(false);
112+
expect(state.errors[expectedHash]).toBe(null);
113+
});
114+
83115
it('should handle fetch failure', async () => {
84116
const diagram = '@startuml\nA --> B\n@enduml';
85117

@@ -185,4 +217,4 @@ describe('plantUMLSlice', () => {
185217
});
186218
});
187219
});
188-
});
220+
});

0 commit comments

Comments
 (0)