Skip to content

Commit

Permalink
[CM] Fix flaky test in the example app (#153310)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant authored Mar 21, 2023
1 parent fbba6fe commit 3872fd6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 deletions.
101 changes: 59 additions & 42 deletions examples/content_management_examples/public/examples/todos/todos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
* Side Public License, v 1.
*/
import React from 'react';
import { EuiButtonGroup, EuiButtonIcon, EuiCheckbox, EuiFieldText, EuiSpacer } from '@elastic/eui';
import {
EuiButtonGroup,
EuiButtonIcon,
EuiCheckbox,
EuiFieldText,
EuiSpacer,
EuiLoadingSpinner,
} from '@elastic/eui';
import {
useCreateContentMutation,
useDeleteContentMutation,
Expand Down Expand Up @@ -55,15 +62,21 @@ const filters = [
export const Todos = () => {
const [filterIdSelected, setFilterIdSelected] = React.useState<TodoFilter>('all');

const { data, isLoading, isError, error } = useSearchTodosQuery({
const { data, isError, error, isFetching, isLoading } = useSearchTodosQuery({
filter: filterIdSelected === 'all' ? undefined : filterIdSelected,
});

const createTodoMutation = useCreateTodoMutation();
const deleteTodoMutation = useDeleteTodoMutation();
const updateTodoMutation = useUpdateTodoMutation();

if (isLoading) return <p>Loading...</p>;
const isPending =
isFetching ||
isLoading ||
createTodoMutation.isLoading ||
deleteTodoMutation.isLoading ||
updateTodoMutation.isLoading;

if (isError) return <p>Error: {error}</p>;

return (
Expand All @@ -77,46 +90,50 @@ export const Todos = () => {
}}
/>
<EuiSpacer />
<ul>
{data.hits.map((todo: Todo) => (
<React.Fragment key={todo.id}>
<li
style={{ display: 'flex', alignItems: 'center' }}
data-test-subj={`todoItem todoItem-${todo.id}`}
>
<EuiCheckbox
id={todo.id + ''}
key={todo.id}
checked={todo.completed}
onChange={(e) => {
updateTodoMutation.mutate({
contentTypeId: TODO_CONTENT_ID,
id: todo.id,
data: {
completed: e.target.checked,
},
});
}}
label={todo.title}
data-test-subj={`todoCheckbox todoCheckbox-${todo.id}`}
/>
{!isLoading && (
<ul>
{data.hits.map((todo: Todo) => (
<React.Fragment key={todo.id}>
<li
style={{ display: 'flex', alignItems: 'center' }}
data-test-subj={`todoItem todoItem-${todo.id}`}
>
<EuiCheckbox
id={todo.id + ''}
key={todo.id}
checked={todo.completed}
onChange={(e) => {
updateTodoMutation.mutate({
contentTypeId: TODO_CONTENT_ID,
id: todo.id,
data: {
completed: e.target.checked,
},
});
}}
label={todo.title}
data-test-subj={`todoCheckbox todoCheckbox-${todo.id}`}
/>

<EuiButtonIcon
style={{ marginLeft: '8px' }}
display="base"
iconType="trash"
aria-label="Delete"
color="danger"
onClick={() => {
deleteTodoMutation.mutate({ contentTypeId: TODO_CONTENT_ID, id: todo.id });
}}
/>
</li>
<EuiSpacer size={'xs'} />
</React.Fragment>
))}
</ul>
<EuiSpacer />
<EuiButtonIcon
style={{ marginLeft: '8px' }}
display="base"
iconType="trash"
aria-label="Delete"
color="danger"
onClick={() => {
deleteTodoMutation.mutate({ contentTypeId: TODO_CONTENT_ID, id: todo.id });
}}
/>
</li>
<EuiSpacer size={'xs'} />
</React.Fragment>
))}
</ul>
)}
<div style={{ minHeight: 24 }}>
{isPending && <EuiLoadingSpinner data-test-subj={'todoPending'} />}
</div>
<form
onSubmit={(e) => {
const inputRef = (e.target as HTMLFormElement).elements.namedItem(
Expand Down
18 changes: 11 additions & 7 deletions test/examples/content_management/todo_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,51 @@ import { PluginFunctionalProviderContext } from '../../plugin_functional/service
export default function ({ getService, getPageObjects }: PluginFunctionalProviderContext) {
const testSubjects = getService('testSubjects');
const find = getService('find');
const retry = getService('retry');
const PageObjects = getPageObjects(['common']);

// FLAKY: https://github.com/elastic/kibana/issues/152852
describe.skip('Todo app', () => {
describe('Todo app', () => {
it('Todo app works', async () => {
const appId = 'contentManagementExamples';
await PageObjects.common.navigateToApp(appId);
await testSubjects.missingOrFail(`todoPending`);

// check that initial state is correct
let todos = await testSubjects.findAll(`~todoItem`);
expect(todos.length).to.be(2);

// check that filters work
await (await find.byCssSelector('label[title="Completed"]')).click();
await testSubjects.missingOrFail(`todoPending`);
todos = await testSubjects.findAll(`~todoItem`);
expect(todos.length).to.be(1);

await (await find.byCssSelector('label[title="Todo"]')).click();
await testSubjects.missingOrFail(`todoPending`);
todos = await testSubjects.findAll(`~todoItem`);
expect(todos.length).to.be(1);

await (await find.byCssSelector('label[title="All"]')).click();
await testSubjects.missingOrFail(`todoPending`);
todos = await testSubjects.findAll(`~todoItem`);
expect(todos.length).to.be(2);

// check that adding new todo works
await testSubjects.setValue('newTodo', 'New todo');
await (await testSubjects.find('newTodo')).pressKeys(Key.ENTER);
await retry.tryForTime(1000, async () => {
todos = await testSubjects.findAll(`~todoItem`);
expect(todos.length).to.be(3);
});
await testSubjects.missingOrFail(`todoPending`);
todos = await testSubjects.findAll(`~todoItem`);
expect(todos.length).to.be(3);

// check that updating todo works
let newTodo = todos[2];
expect(await newTodo.getVisibleText()).to.be('New todo');
let newTodoCheckbox = await newTodo.findByTestSubject('~todoCheckbox');
expect(await newTodoCheckbox.isSelected()).to.be(false);
await (await newTodo.findByTagName('label')).click();
await testSubjects.missingOrFail(`todoPending`);

await (await find.byCssSelector('label[title="Completed"]')).click();
await testSubjects.missingOrFail(`todoPending`);
todos = await testSubjects.findAll(`~todoItem`);
expect(todos.length).to.be(2);
newTodo = todos[1];
Expand All @@ -66,6 +69,7 @@ export default function ({ getService, getPageObjects }: PluginFunctionalProvide

// check that deleting todo works
await (await newTodo.findByCssSelector('[aria-label="Delete"]')).click();
await testSubjects.missingOrFail(`todoPending`);
todos = await testSubjects.findAll(`~todoItem`);
expect(todos.length).to.be(1);
});
Expand Down

0 comments on commit 3872fd6

Please sign in to comment.