Skip to content

Commit 953f3db

Browse files
committed
Fix tests and logic
1 parent ae552da commit 953f3db

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

site/src/pages/TasksPage/TasksPage.stories.tsx

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Meta, StoryObj } from "@storybook/react";
22
import { expect, spyOn, userEvent, within } from "@storybook/test";
3+
import { API } from "api/api";
4+
import { MockUsers } from "pages/UsersPage/storybookData/users";
35
import {
46
MockTemplate,
57
MockUserOwner,
@@ -21,6 +23,12 @@ const meta: Meta<typeof TasksPage> = {
2123
parameters: {
2224
user: MockUserOwner,
2325
},
26+
beforeEach: () => {
27+
spyOn(API, "getUsers").mockResolvedValue({
28+
users: MockUsers,
29+
count: MockUsers.length,
30+
});
31+
},
2432
};
2533

2634
export default meta;
@@ -62,7 +70,8 @@ export const LoadingTasks: Story = {
6270
const canvas = within(canvasElement);
6371

6472
await step("Select the first AI template", async () => {
65-
const combobox = await canvas.findByRole("combobox");
73+
const form = await canvas.findByRole("form");
74+
const combobox = await within(form).findByRole("combobox");
6675
expect(combobox).toHaveTextContent(MockTemplate.display_name);
6776
});
6877
},
@@ -94,37 +103,40 @@ export const LoadedTasks: Story = {
94103
},
95104
};
96105

106+
const newTaskData = {
107+
prompt: "Create a new task",
108+
workspace: {
109+
...MockWorkspace,
110+
id: "workspace-4",
111+
latest_app_status: {
112+
...MockWorkspaceAppStatus,
113+
message: "Task created successfully!",
114+
},
115+
},
116+
};
117+
97118
export const CreateTaskSuccessfully: Story = {
98119
decorators: [withProxyProvider()],
99120
beforeEach: () => {
100121
spyOn(data, "fetchAITemplates").mockResolvedValue([MockTemplate]);
101-
spyOn(data, "fetchTasks").mockResolvedValue(MockTasks);
102-
spyOn(data, "createTask").mockImplementation((prompt: string) => {
103-
return Promise.resolve({
104-
prompt,
105-
workspace: {
106-
...MockWorkspace,
107-
latest_app_status: {
108-
...MockWorkspaceAppStatus,
109-
message: "Task created successfully!",
110-
},
111-
},
112-
});
113-
});
122+
spyOn(data, "fetchTasks")
123+
.mockResolvedValueOnce(MockTasks)
124+
.mockResolvedValue([newTaskData, ...MockTasks]);
125+
spyOn(data, "createTask").mockResolvedValue(newTaskData);
114126
},
115127
play: async ({ canvasElement, step }) => {
116128
const canvas = within(canvasElement);
117129

118130
await step("Run task", async () => {
119131
const prompt = await canvas.findByLabelText(/prompt/i);
120-
await userEvent.type(prompt, "Create a new task");
132+
await userEvent.type(prompt, newTaskData.prompt);
121133
const submitButton = canvas.getByRole("button", { name: /run task/i });
122134
await userEvent.click(submitButton);
123135
});
124136

125137
await step("Verify task in the table", async () => {
126138
await canvas.findByRole("row", {
127-
name: /create a new task/i,
139+
name: new RegExp(newTaskData.prompt, "i"),
128140
});
129141
});
130142
},

site/src/pages/TasksPage/TasksPage.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const TasksPage: FC = () => {
7777
const [filter, setFilter] = useState<TasksFilter>({
7878
user: {
7979
value: user.username,
80-
label: user.name ?? user.username,
80+
label: user.name || user.username,
8181
avatarUrl: user.avatar_url,
8282
},
8383
});
@@ -179,12 +179,9 @@ const TaskForm: FC<TaskFormProps> = ({ templates }) => {
179179
const createTaskMutation = useMutation({
180180
mutationFn: async ({ prompt, templateId }: CreateTaskMutationFnProps) =>
181181
data.createTask(prompt, user.id, templateId),
182-
onSuccess: (newTask) => {
183-
// The current data loading is heavy, so we manually update the cache to
184-
// avoid re-fetching. Once we improve data loading, we can replace the
185-
// manual update with queryClient.invalidateQueries.
186-
queryClient.setQueryData<Task[]>(["tasks"], (oldTasks = []) => {
187-
return [newTask, ...oldTasks];
182+
onSuccess: async () => {
183+
await queryClient.invalidateQueries({
184+
queryKey: ["tasks"],
188185
});
189186
},
190187
});
@@ -218,6 +215,7 @@ const TaskForm: FC<TaskFormProps> = ({ templates }) => {
218215
<form
219216
className="border border-border border-solid rounded-lg p-4"
220217
onSubmit={onSubmit}
218+
aria-label="Create AI task"
221219
>
222220
<fieldset disabled={createTaskMutation.isPending}>
223221
<label htmlFor="prompt" className="sr-only">

site/src/pages/TasksPage/UsersCombobox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const UsersCombobox: FC<UsersComboboxProps> = ({
4949
});
5050

5151
const options = usersQuery.data?.map((user) => ({
52-
label: user.name ?? user.username,
52+
label: user.name || user.username,
5353
value: user.username,
5454
avatarUrl: user.avatar_url,
5555
}));

0 commit comments

Comments
 (0)