Skip to content

Commit 2a2f35a

Browse files
committed
Support error
1 parent 7436c97 commit 2a2f35a

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

site/src/@types/storybook.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ declare module "@storybook/react" {
77
features?: FeatureName[];
88
experiments?: Experiments;
99
queries?: { key: QueryKey; data: unknown }[];
10-
webSocket?: {
11-
messages: string[];
12-
};
10+
webSocket?:
11+
| {
12+
event: "message";
13+
messages: string[];
14+
}
15+
| { event: "error" };
1316
}
1417
}

site/src/pages/CreateTemplatePage/BuildLogsDrawer.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const Logs: Story = {
4747
decorators: [withWebSocket],
4848
parameters: {
4949
webSocket: {
50+
event: "message",
5051
messages: MockWorkspaceBuildLogs.map((log) => JSON.stringify(log)),
5152
},
5253
},

site/src/pages/TerminalPage/TerminalPage.stories.tsx

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const meta = {
4949
{ key: ["entitlements"], data: MockEntitlements },
5050
{ key: ["experiments"], data: MockExperiments },
5151
{ key: ["appearance"], data: MockAppearanceConfig },
52-
5352
{
5453
key: getAuthorizationKey({ checks: permissionsToCheck }),
5554
data: { editWorkspaceProxies: true },
@@ -68,36 +67,44 @@ const meta = {
6867
export default meta;
6968
type Story = StoryObj<typeof TerminalPage>;
7069

71-
export const Connected: Story = {
70+
const readyWorkspaceQuery = {
71+
key: workspaceByOwnerAndNameKey(MockWorkspace.owner_name, MockWorkspace.name),
72+
data: {
73+
...MockWorkspace,
74+
latest_build: {
75+
...MockWorkspace.latest_build,
76+
resources: [
77+
{
78+
...MockWorkspace.latest_build.resources[0],
79+
agents: [{ ...MockWorkspaceAgent, lifecycle_state: "ready" }],
80+
},
81+
],
82+
},
83+
} satisfies Workspace,
84+
};
85+
86+
export const OnMessage: Story = {
7287
decorators: [withWebSocket],
7388
parameters: {
7489
...meta.parameters,
7590
webSocket: {
91+
event: "message",
7692
// Copied and pasted this from browser
7793
messages: [
7894
`➜ codergit:(bq/refactor-web-term-notifications) ✗`,
7995
],
8096
},
81-
queries: [
82-
...meta.parameters.queries,
83-
{
84-
key: workspaceByOwnerAndNameKey(
85-
MockWorkspace.owner_name,
86-
MockWorkspace.name,
87-
),
88-
data: {
89-
...MockWorkspace,
90-
latest_build: {
91-
...MockWorkspace.latest_build,
92-
resources: [
93-
{
94-
...MockWorkspace.latest_build.resources[0],
95-
agents: [{ ...MockWorkspaceAgent, lifecycle_state: "ready" }],
96-
},
97-
],
98-
},
99-
} satisfies Workspace,
100-
},
101-
],
97+
queries: [...meta.parameters.queries, readyWorkspaceQuery],
98+
},
99+
};
100+
101+
export const OnError: Story = {
102+
decorators: [withWebSocket],
103+
parameters: {
104+
...meta.parameters,
105+
webSocket: {
106+
event: "error",
107+
},
108+
queries: [...meta.parameters.queries, readyWorkspaceQuery],
102109
},
103110
};

site/src/testHelpers/storybook.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,28 @@ export const withDashboardProvider = (
4646
};
4747

4848
export const withWebSocket = (Story: FC, { parameters }: StoryContext) => {
49-
if (!parameters.webSocket) {
50-
console.warn(
51-
"Looks like you forgot to add websocket messages to the story",
52-
);
49+
const webSocketConfig = parameters.webSocket;
50+
51+
if (!webSocketConfig) {
52+
console.warn("Your forgot to add the `parameters.webSocket` to your story");
53+
return <Story />;
5354
}
5455

5556
// @ts-expect-error -- TS doesn't know about the global WebSocket
5657
window.WebSocket = function () {
5758
return {
5859
addEventListener: (
5960
type: string,
60-
callback: (ev: Record<"data", string>) => void,
61+
callback: (ev?: Record<"data", string>) => void,
6162
) => {
62-
if (type === "message") {
63-
parameters.webSocket?.messages.forEach((message) => {
63+
if (type === "message" && webSocketConfig.event === "message") {
64+
webSocketConfig.messages.forEach((message) => {
6465
callback({ data: message });
6566
});
6667
}
68+
if (type === "error" && webSocketConfig.event === "error") {
69+
callback();
70+
}
6771
},
6872
close: () => {},
6973
};

0 commit comments

Comments
 (0)