Skip to content

Commit b4ec009

Browse files
committed
fix tests
1 parent 62904c1 commit b4ec009

File tree

3 files changed

+54
-31
lines changed

3 files changed

+54
-31
lines changed

static/app/components/onboarding/gettingStartedDoc/utils/useCurrentProjectState.spec.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {createMemoryRouter, RouterProvider} from 'react-router-dom';
22
import {ProjectFixture} from 'sentry-fixture/project';
33

4+
import {SentryNuqsTestingAdapter} from 'sentry-test/nuqsTestingAdapter';
45
import {act, renderHook} from 'sentry-test/reactTestingLibrary';
56

67
import {useCurrentProjectState} from 'sentry/components/onboarding/gettingStartedDoc/utils/useCurrentProjectState';
@@ -16,14 +17,15 @@ import type {Project} from 'sentry/types/project';
1617

1718
function createWrapper(projectSlug?: string) {
1819
return function Wrapper({children}: any) {
20+
const wrapped = <SentryNuqsTestingAdapter>{children}</SentryNuqsTestingAdapter>;
1921
const memoryRouter = createMemoryRouter([
2022
{
2123
path: '/',
22-
element: children,
24+
element: wrapped,
2325
},
2426
{
2527
path: '/:projectId/',
26-
element: children,
28+
element: wrapped,
2729
},
2830
]);
2931

static/app/utils/replays/hooks/useActiveReplayTab.spec.tsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {AutofixSetupFixture} from 'sentry-fixture/autofixSetupFixture';
22
import {OrganizationFixture} from 'sentry-fixture/organization';
33

4-
import {act, renderHookWithProviders} from 'sentry-test/reactTestingLibrary';
4+
import {act, renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary';
55
import {setWindowLocation} from 'sentry-test/utils';
66

77
import {TabKey, useActiveReplayTab} from 'sentry/utils/replays/hooks/useActiveReplayTab';
@@ -40,17 +40,25 @@ describe('useActiveReplayTab', () => {
4040
expect(result.current.getActiveTab()).toBe(TabKey.AI);
4141
});
4242

43-
it('should set the default tab if the name is invalid', () => {
43+
it('should set the default tab if the name is invalid', async () => {
4444
const {result, router} = renderHookWithProviders(useActiveReplayTab, {
4545
initialProps: {},
46+
initialRouterConfig: {
47+
location: {pathname: '/mock-pathname/', query: {query: 'click.tag:button'}},
48+
},
4649
organization: OrganizationFixture({
4750
features: ['gen-ai-features', 'replay-ai-summaries'],
4851
}),
4952
});
5053
expect(result.current.getActiveTab()).toBe(TabKey.AI);
5154

5255
act(() => result.current.setActiveTab('foo bar'));
53-
expect(router.location.query).toEqual({query: 'click.tag:button', t_main: 'ai'});
56+
await waitFor(() => {
57+
expect(router.location.query).toEqual({
58+
query: 'click.tag:button',
59+
t_main: 'ai',
60+
});
61+
});
5462
});
5563

5664
it('should use AI as default for video replays when replay-ai-summaries-mobile is enabled', () => {
@@ -103,34 +111,43 @@ describe('useActiveReplayTab', () => {
103111
expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
104112
});
105113

106-
it('should set the default tab if the name is invalid', () => {
114+
it('should set the default tab if the name is invalid', async () => {
107115
const {result, router} = renderHookWithProviders(useActiveReplayTab, {
108116
initialProps: {},
117+
initialRouterConfig: {
118+
location: {pathname: '/mock-pathname/', query: {query: 'click.tag:button'}},
119+
},
109120
organization: OrganizationFixture({features: []}),
110121
});
111122
expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
112123

113124
act(() => result.current.setActiveTab('foo bar'));
114-
expect(router.location.query).toEqual({
115-
query: 'click.tag:button',
116-
t_main: 'breadcrumbs',
125+
await waitFor(() => {
126+
expect(router.location.query).toEqual({
127+
query: 'click.tag:button',
128+
t_main: 'breadcrumbs',
129+
});
117130
});
118131
});
119132
});
120133
});
121134

122-
it('should allow case-insensitive tab names', () => {
135+
it('should allow case-insensitive tab names', async () => {
123136
const {result, router} = renderHookWithProviders(useActiveReplayTab, {
124137
initialProps: {},
138+
initialRouterConfig: {
139+
location: {pathname: '/mock-pathname/', query: {query: 'click.tag:button'}},
140+
},
125141
organization: OrganizationFixture({features: []}),
126142
});
127143
expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
128144

129145
act(() => result.current.setActiveTab('nEtWoRk'));
130-
131-
expect(router.location.query).toEqual({
132-
query: 'click.tag:button',
133-
t_main: 'network',
146+
await waitFor(() => {
147+
expect(router.location.query).toEqual({
148+
query: 'click.tag:button',
149+
t_main: 'network',
150+
});
134151
});
135152
});
136153
});

static/app/utils/replays/hooks/useActiveReplayTab.tsx

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useCallback} from 'react';
2-
import {parseAsStringLiteral, useQueryState} from 'nuqs';
2+
import {createParser, parseAsStringLiteral, useQueryState} from 'nuqs';
33

44
import {useOrganizationSeerSetup} from 'sentry/components/events/autofix/useOrganizationSeerSetup';
55
import {defined} from 'sentry/utils';
@@ -10,34 +10,38 @@ export enum TabKey {
1010
BREADCRUMBS = 'breadcrumbs',
1111
CONSOLE = 'console',
1212
ERRORS = 'errors',
13+
LOGS = 'logs',
1314
MEMORY = 'memory',
1415
NETWORK = 'network',
16+
PLAYLIST = 'playlist',
1517
TAGS = 'tags',
1618
TRACE = 'trace',
17-
LOGS = 'logs',
18-
PLAYLIST = 'playlist',
1919
}
2020

2121
function isReplayTab({tab, isVideoReplay}: {isVideoReplay: boolean; tab: string}) {
22-
const supportedTabs = [
23-
TabKey.AI,
24-
TabKey.BREADCRUMBS,
25-
TabKey.CONSOLE,
26-
TabKey.ERRORS,
27-
TabKey.LOGS,
28-
TabKey.NETWORK,
29-
TabKey.PLAYLIST,
30-
TabKey.TAGS,
31-
TabKey.TRACE,
32-
];
33-
3422
if (isVideoReplay) {
35-
return supportedTabs.includes(tab as TabKey);
23+
const supportedVideoTabs = [
24+
TabKey.AI,
25+
TabKey.BREADCRUMBS,
26+
TabKey.CONSOLE,
27+
TabKey.ERRORS,
28+
TabKey.LOGS,
29+
TabKey.NETWORK,
30+
TabKey.PLAYLIST,
31+
TabKey.TAGS,
32+
TabKey.TRACE,
33+
];
34+
return supportedVideoTabs.includes(tab as TabKey);
3635
}
3736

3837
return Object.values<string>(TabKey).includes(tab);
3938
}
4039

40+
const tabKeyParser = createParser<TabKey>({
41+
parse: value => parseAsStringLiteral(Object.values(TabKey)).parse(value.toLowerCase()),
42+
serialize: value => value,
43+
});
44+
4145
export function useActiveReplayTab({isVideoReplay = false}: {isVideoReplay?: boolean}) {
4246
const organization = useOrganization();
4347
const {areAiFeaturesAllowed} = useOrganizationSeerSetup();
@@ -54,7 +58,7 @@ export function useActiveReplayTab({isVideoReplay = false}: {isVideoReplay?: boo
5458

5559
const [tabParam, setTabParam] = useQueryState(
5660
't_main',
57-
parseAsStringLiteral(Object.values(TabKey))
61+
tabKeyParser.withDefault(defaultTab).withOptions({clearOnDefault: false})
5862
);
5963

6064
return {

0 commit comments

Comments
 (0)