Skip to content

Commit 3586d87

Browse files
committed
Fix unit tests
1 parent 4b30652 commit 3586d87

File tree

4 files changed

+72
-74
lines changed

4 files changed

+72
-74
lines changed

web_ui/src/core/jobs/hooks/utils.test.tsx

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (C) 2022-2025 Intel Corporation
22
// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE
33

4-
import { QueryClient } from '@tanstack/react-query';
4+
import { useQueryClient } from '@tanstack/react-query';
55
import { waitFor } from '@testing-library/react';
66

77
import { getMockedWorkspaceIdentifier } from '../../../test-utils/mocked-items-factory/mocked-identifiers';
@@ -36,32 +36,39 @@ describe('Use jobs hook utils', () => {
3636
});
3737

3838
it('Should not invalidate balance if feature flag is disabled', async () => {
39-
const queryClient = new QueryClient();
40-
queryClient.invalidateQueries = jest.fn();
39+
const queryClient = renderHookWithProviders(useQueryClient);
40+
queryClient.result.current.invalidateQueries = jest.fn();
41+
4142
renderHookWithProviders(
42-
() =>
43-
useInvalidateBalanceOnNewJob(
43+
() => {
44+
return useInvalidateBalanceOnNewJob(
4445
workspaceIdentifier,
4546
getMockedResponse([getMockedJob({ cost: { leaseId: '123', requests: [], consumed: [] } })]),
4647
{ jobState: JobState.SCHEDULED }
47-
),
48+
);
49+
},
4850
{
49-
providerProps: { queryClient, featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: false } },
51+
providerProps: { featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: false } },
5052
}
5153
);
5254

5355
await waitFor(() => {
54-
expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
56+
expect(queryClient.result.current.invalidateQueries).not.toHaveBeenCalled();
5557
});
5658
});
5759

5860
it('Should invalidate balance if feature flag is enabled', async () => {
59-
const queryClient = new QueryClient();
60-
queryClient.invalidateQueries = jest.fn();
61+
let invalidateSpy: jest.SpyInstance | undefined;
62+
6163
const { rerender } = renderHookWithProviders(
62-
({ jobs }) => useInvalidateBalanceOnNewJob(workspaceIdentifier, getMockedResponse(jobs), {}),
64+
({ jobs }) => {
65+
const queryClient = useQueryClient();
66+
invalidateSpy = jest.spyOn(queryClient, 'invalidateQueries');
67+
68+
return useInvalidateBalanceOnNewJob(workspaceIdentifier, getMockedResponse(jobs), {});
69+
},
6370
{
64-
providerProps: { queryClient, featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: true } },
71+
providerProps: { featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: true } },
6572
initialProps: {
6673
jobs: [
6774
getMockedJob({
@@ -87,54 +94,57 @@ describe('Use jobs hook utils', () => {
8794
});
8895

8996
await waitFor(() => {
90-
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(2);
97+
expect(invalidateSpy).toHaveBeenCalledTimes(2);
9198
});
9299
});
93100

94101
it('Should not invalidate balance if there are no jobs', async () => {
95-
const queryClient = new QueryClient();
96-
queryClient.invalidateQueries = jest.fn();
102+
const queryClient = renderHookWithProviders(useQueryClient);
103+
queryClient.result.current.invalidateQueries = jest.fn();
97104

98105
renderHookWithProviders(
99106
() =>
100107
useInvalidateBalanceOnNewJob(workspaceIdentifier, getMockedResponse([]), {
101108
jobState: JobState.SCHEDULED,
102109
}),
103-
{ providerProps: { queryClient, featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: true } } }
110+
{ providerProps: { featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: true } } }
104111
);
105112

106113
await waitFor(() => {
107-
expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
114+
expect(queryClient.result.current.invalidateQueries).not.toHaveBeenCalled();
108115
});
109116
});
110117

111118
it('Should not invalidate balance if there are no jobs with cost', async () => {
112-
const queryClient = new QueryClient();
113-
queryClient.invalidateQueries = jest.fn();
119+
const queryClient = renderHookWithProviders(useQueryClient);
120+
queryClient.result.current.invalidateQueries = jest.fn();
114121
renderHookWithProviders(
115122
() =>
116123
useInvalidateBalanceOnNewJob(workspaceIdentifier, getMockedResponse([getMockedJob()]), {
117124
jobState: JobState.SCHEDULED,
118125
}),
119-
{ providerProps: { queryClient, featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: true } } }
126+
{ providerProps: { featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: true } } }
120127
);
121128

122129
await waitFor(() => {
123-
expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
130+
expect(queryClient.result.current.invalidateQueries).not.toHaveBeenCalled();
124131
});
125132
});
126133

127134
it('Should invalidate balance if there is a job with new id or a new job', async () => {
128-
const queryClient = new QueryClient();
129-
queryClient.invalidateQueries = jest.fn();
135+
let invalidateSpy: jest.SpyInstance | undefined;
136+
130137
const { rerender } = renderHookWithProviders(
131-
({ jobs }) =>
132-
useInvalidateBalanceOnNewJob(workspaceIdentifier, getMockedResponse(jobs), {
138+
({ jobs }) => {
139+
const queryClient = useQueryClient();
140+
invalidateSpy = jest.spyOn(queryClient, 'invalidateQueries');
141+
142+
return useInvalidateBalanceOnNewJob(workspaceIdentifier, getMockedResponse(jobs), {
133143
jobState: JobState.SCHEDULED,
134-
}),
144+
});
145+
},
135146
{
136147
providerProps: {
137-
queryClient,
138148
featureFlags: { FEATURE_FLAG_CREDIT_SYSTEM: true },
139149
},
140150
initialProps: {
@@ -149,7 +159,7 @@ describe('Use jobs hook utils', () => {
149159
);
150160

151161
await waitFor(() => {
152-
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(1);
162+
expect(invalidateSpy).toHaveBeenCalledTimes(1);
153163
});
154164

155165
rerender({
@@ -163,7 +173,7 @@ describe('Use jobs hook utils', () => {
163173
});
164174

165175
await waitFor(() => {
166-
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(2);
176+
expect(invalidateSpy).toHaveBeenCalledTimes(2);
167177
});
168178

169179
rerender({
@@ -178,7 +188,7 @@ describe('Use jobs hook utils', () => {
178188
});
179189

180190
await waitFor(() => {
181-
expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(3);
191+
expect(invalidateSpy).toHaveBeenCalledTimes(3);
182192
});
183193
});
184194
});

web_ui/src/notification/notification.component.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type addToastNotificationProps = Omit<NotificationToastProps, 'remove'> & {
2424
placement?: NOTIFICATION_CONTAINER;
2525
};
2626

27-
interface addNotificationProps {
27+
export interface AddNotificationProps {
2828
message: ReactChild;
2929
type: NOTIFICATION_TYPE;
3030
dismiss?: DismissOptions;
@@ -35,7 +35,7 @@ interface addNotificationProps {
3535
interface NotificationContextProps {
3636
removeNotification: (id: string) => void;
3737
removeNotifications: () => void;
38-
addNotification: ({ message, type, dismiss, actionButtons }: addNotificationProps) => string;
38+
addNotification: ({ message, type, dismiss, actionButtons }: AddNotificationProps) => string;
3939
addToastNotification: (data: addToastNotificationProps) => string;
4040
}
4141

@@ -70,7 +70,7 @@ export const NotificationProvider = ({ children }: NotificationProviderProps): J
7070
hasCloseButton = true,
7171
dismiss = DEFAULT_DISMISS_OPTIONS,
7272
actionButtons,
73-
}: addNotificationProps): string => {
73+
}: AddNotificationProps): string => {
7474
const notificationId = isString(message) || isNumber(message) ? `id-${message}` : `id-${message.key}`;
7575

7676
const NotificationContainer: iNotification = {

web_ui/src/routes/organizations/organizations-context.test.tsx

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
OnboardingService,
88
OrganizationMetadata,
99
} from '@geti/core/src/users/services/onboarding-service.interface';
10-
import { screen, waitForElementToBeRemoved } from '@testing-library/react';
10+
import { screen, waitFor } from '@testing-library/react';
1111
import { AxiosError, HttpStatusCode } from 'axios';
1212

1313
import { AccountStatus } from '../../core/organizations/organizations.interface';
@@ -51,10 +51,9 @@ const suspendedOrganization = {
5151

5252
const renderApp = async ({
5353
profile = null,
54-
onboardingService,
54+
onboardingService = createInMemoryOnboardingService(),
5555
}: {
5656
profile?: OnboardingProfile | null;
57-
isError?: boolean;
5857
onboardingService?: OnboardingService;
5958
organizations?: OrganizationMetadata[];
6059
selectedOrganization?: OrganizationMetadata | null;
@@ -67,22 +66,15 @@ const renderApp = async ({
6766
</ErrorBoundary>,
6867
{ profile, services: { onboardingService } }
6968
);
70-
71-
await waitForElementToBeRemoved(screen.getByRole('progressbar'));
7269
};
7370

7471
describe('Organizations context', () => {
7572
it('Displays <SuspendedOrganization /> if the organization has been suspended or deleted.', async () => {
76-
const onboardingService = createInMemoryOnboardingService();
77-
onboardingService.getActiveUserProfile = jest.fn(() =>
78-
Promise.resolve({
73+
await renderApp({
74+
profile: {
7975
organizations: [suspendedOrganization],
8076
hasAcceptedUserTermsAndConditions: true,
81-
})
82-
);
83-
84-
await renderApp({
85-
onboardingService,
77+
},
8678
});
8779

8880
expect(screen.getByText(/Your organization's account has been suspended/)).toBeVisible();
@@ -104,22 +96,21 @@ describe('Organizations context', () => {
10496

10597
await renderApp({
10698
onboardingService,
99+
profile: null,
107100
});
108101

109-
expect(screen.getByText(/You do not have access to any Intel Geti organization/)).toBeVisible();
102+
await waitFor(() => {
103+
expect(screen.getByText(/You do not have access to any Intel Geti organization/)).toBeVisible();
104+
});
110105
});
111106

112107
describe('multiple organizations', () => {
113108
it('Displays the "OrganizationSelectionModal" when no organization has been selected', async () => {
114-
const onboardingService = createInMemoryOnboardingService();
115-
onboardingService.getActiveUserProfile = jest.fn(() =>
116-
Promise.resolve({
109+
await renderApp({
110+
profile: {
117111
organizations: [mockedOrganization, mockedOrganizationTwo],
118112
hasAcceptedUserTermsAndConditions: true,
119-
})
120-
);
121-
await renderApp({
122-
onboardingService,
113+
},
123114
});
124115

125116
expect(screen.getByText(/^You belong to the following organizations./i)).toBeVisible();
@@ -131,16 +122,11 @@ describe('Organizations context', () => {
131122
});
132123

133124
it('Displays <OrganizationSelectionModal /> if the organization has been suspended.', async () => {
134-
const onboardingService = createInMemoryOnboardingService();
135-
onboardingService.getActiveUserProfile = jest.fn(() =>
136-
Promise.resolve({
125+
await renderApp({
126+
profile: {
137127
organizations: [suspendedOrganization, mockedOrganization],
138128
hasAcceptedUserTermsAndConditions: true,
139-
})
140-
);
141-
142-
await renderApp({
143-
onboardingService,
129+
},
144130
});
145131

146132
expect(

web_ui/src/test-utils/required-providers-render.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ const PrefilledQueryClientProvider = ({
5959
...featureFlags,
6060
});
6161

62-
if (profile !== null && profile !== undefined) {
62+
if (profile !== null) {
6363
client.setQueryData(QUERY_KEYS.USER_ONBOARDING_PROFILE, {
64-
...profile,
6564
organizations: [{ id: prefilledOrgId, status: AccountStatusDTO.ACTIVE }],
6665
hasAcceptedUserTermsAndConditions: true,
66+
...profile,
6767
});
6868
}
6969

@@ -92,18 +92,20 @@ export const RequiredProviders = ({
9292
return (
9393
<Suspense fallback={<IntelBrandedLoading />}>
9494
<Router initialEntries={initialEntries}>
95-
<AuthProvider>
96-
<NotificationProvider>
97-
<Notifications />
98-
<PrefilledQueryClientProvider featureFlags={featureFlags} profile={profile}>
99-
<ThemeProvider theme={defaultTheme}>
95+
<NotificationProvider>
96+
<Notifications />
97+
<PrefilledQueryClientProvider featureFlags={featureFlags} profile={profile}>
98+
<ThemeProvider theme={defaultTheme}>
99+
<Suspense fallback={<IntelBrandedLoading />}>
100100
<ApplicationServicesProvider useInMemoryEnvironment {...services}>
101-
<TusUploadProvider>{children}</TusUploadProvider>
101+
<AuthProvider>
102+
<TusUploadProvider>{children}</TusUploadProvider>
103+
</AuthProvider>
102104
</ApplicationServicesProvider>
103-
</ThemeProvider>
104-
</PrefilledQueryClientProvider>
105-
</NotificationProvider>
106-
</AuthProvider>
105+
</Suspense>
106+
</ThemeProvider>
107+
</PrefilledQueryClientProvider>
108+
</NotificationProvider>
107109
</Router>
108110
</Suspense>
109111
);

0 commit comments

Comments
 (0)