forked from skupperproject/openshift-site-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseLinks.spec.ts
More file actions
148 lines (119 loc) · 4.56 KB
/
useLinks.spec.ts
File metadata and controls
148 lines (119 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { createElement } from 'react';
import { RESTApi } from '@API/REST.api';
import { render, act } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { AccessGrantCrdResponse } from '../src/console/interfaces/CRD_AccessGrant';
import { useLinks } from '../src/console/pages/tabs/Links/hooks/useLinks';
const mockUseWatchedSkupperResource = vi.hoisted(() => vi.fn());
const mockCreateObjectURL = vi.hoisted(() => vi.fn());
vi.mock('../src/console/hooks/useSkupperWatchResource', () => ({
useWatchedSkupperResource: mockUseWatchedSkupperResource
}));
vi.mock('../src/console/API/REST.api');
function renderHookTest<TResult>(callback: () => TResult) {
const values: { current: TResult } = { current: null! };
const TestComponent = function () {
values.current = callback();
return null;
};
return {
rerender: () => {
act(() => {
render(createElement(TestComponent));
});
},
result: () => values.current
};
}
describe('useLinks', () => {
const mockData = {
accessGrants: [
{
metadata: { name: 'test-grant' },
status: { status: 'Ready', url: 'u', code: 'c', ca: 'ca', message: 'OK' }
}
],
accessTokens: [
{
name: 'test-token',
status: { message: 'OK' }
}
],
links: [
{
name: 'test-link'
}
],
sites: [
{
remoteLinks: ['remote-site']
}
]
};
beforeEach(() => {
vi.clearAllMocks();
mockUseWatchedSkupperResource
.mockReturnValueOnce({ data: mockData.accessGrants, loaded: true })
.mockReturnValueOnce({ data: mockData.accessTokens, loaded: true })
.mockReturnValueOnce({ data: mockData.links, loaded: true })
.mockReturnValueOnce({ data: mockData.sites, loaded: true });
global.URL.createObjectURL = mockCreateObjectURL;
mockCreateObjectURL.mockReturnValue('mock-url');
});
it('returns data and loading state', () => {
const { rerender, result } = renderHookTest(() => useLinks());
rerender();
expect(result().loading).toBe(false);
expect(result().data.accessGrants).toEqual(mockData.accessGrants);
expect(result().data.accessTokens).toEqual(mockData.accessTokens);
expect(result().data.links).toEqual(mockData.links);
expect(result().data.remoteLinks).toEqual([{ connectedTo: 'remote-site' }]);
});
it('handles delete link', async () => {
vi.mocked(RESTApi.deleteLink).mockResolvedValue();
vi.mocked(RESTApi.deleteAccessToken).mockResolvedValue();
const { rerender, result } = renderHookTest(() => useLinks());
rerender();
await act(async () => {
result().actions.handleDeleteLink('test-token');
});
expect(RESTApi.deleteAccessToken).toHaveBeenCalledWith('test-token');
expect(RESTApi.deleteLink).toHaveBeenCalledWith('test-token');
});
it('handles download grant', () => {
const mockLink = document.createElement('a');
const mockClick = vi.fn();
Object.defineProperty(mockLink, 'click', { value: mockClick });
const mockCreateElement = vi.spyOn(document, 'createElement');
const mockAppendChild = vi.spyOn(document.body, 'appendChild');
const mockRemoveChild = vi.spyOn(document.body, 'removeChild');
mockCreateElement.mockReturnValue(mockLink);
mockAppendChild.mockImplementation(() => mockLink);
mockRemoveChild.mockImplementation(() => mockLink);
const { rerender, result } = renderHookTest(() => useLinks());
rerender();
act(() => result().actions.handleDownloadGrant(mockData.accessGrants[0] as AccessGrantCrdResponse));
expect(mockCreateElement).toHaveBeenCalledWith('a');
expect(mockAppendChild).toHaveBeenCalled();
expect(mockRemoveChild).toHaveBeenCalled();
expect(mockClick).toHaveBeenCalled();
});
it('handles delete link with partial name match', async () => {
mockUseWatchedSkupperResource
.mockReturnValueOnce({ data: mockData.accessGrants, loaded: true })
.mockReturnValueOnce({
data: [{ name: 'partial-token' }],
loaded: true
})
.mockReturnValueOnce({ data: mockData.links, loaded: true })
.mockReturnValueOnce({ data: mockData.sites, loaded: true });
vi.mocked(RESTApi.deleteLink).mockResolvedValue();
vi.mocked(RESTApi.deleteAccessToken).mockResolvedValue();
const { rerender, result } = renderHookTest(() => useLinks());
rerender();
await act(async () => {
result().actions.handleDeleteLink('test-link-with-partial-token');
});
expect(RESTApi.deleteLink).toHaveBeenCalledWith('test-link-with-partial-token');
});
});