Skip to content

Commit

Permalink
[Dashboard] Fix flaky Dashboard create tests (#156085)
Browse files Browse the repository at this point in the history
Closes #155777

## Summary

The `"replaces panel with incoming embeddable if id matches existing
panel"` test was **not** the flaky one, even though it was the one that
showed the failure; it was actually one of the previous `"pulls state
<...>"` tests that was failing, which was pretty confusing to catch 🤦

Basically, because we are [running the unsaved changes check on load
now](#155648), depending on the
timing of the `debounce` on the `checkForUnsavedChangesSubject$`
subscription it would sometimes run for the `"pulls state <...>"` tests
(but not always) - so whenever it **would** run, because the mocked
`loadDashboardStateFromSavedObject` was only returning a partial
Dashboard input, this would result in trying to get the property of
`undefined` when checking for filter changes, panel changes, etc.

This is fixed by ensuring that the `loadDashboardStateFromSavedObject`
returns a complete Dashboard input, with all the required keys, for all
of the relevant tests. Note that, since you can't test Jest unit tests
using the flaky test runner, I was able to run it multiple times by
surrounding all the tests with the following in order to ensure that it
was no longer flaky:

```typescript
for (const i of Array(x)
  .fill(null)
  .map((_, i) => i)) {
  describe(`test run ${i}`, () => {
    <...> // all the tests go here
  });
};
```

I did this with `x = 200`, and the entire test suite passed 200 times in
a row 👍

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
  • Loading branch information
Heenawter authored Apr 28, 2023
1 parent 19f5312 commit 5779773
Showing 1 changed file with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

import { Observable } from 'rxjs';

import {
ContactCardEmbeddable,
ContactCardEmbeddableFactory,
Expand All @@ -26,7 +28,7 @@ import { createDashboard } from './create_dashboard';
import { getSampleDashboardPanel } from '../../../mocks';
import { pluginServices } from '../../../services/plugin_services';
import { DashboardCreationOptions } from '../dashboard_container_factory';
import { Observable } from 'rxjs';
import { DEFAULT_DASHBOARD_INPUT } from '../../../dashboard_constants';

const embeddableId = 'create-dat-dashboard';

Expand Down Expand Up @@ -54,18 +56,28 @@ test('throws error when provided validation function returns invalid', async ()
test('pulls state from dashboard saved object when given a saved object id', async () => {
pluginServices.getServices().dashboardSavedObject.loadDashboardStateFromSavedObject = jest
.fn()
.mockResolvedValue({ dashboardInput: { description: 'wow would you look at that? Wow.' } });
.mockResolvedValue({
dashboardInput: {
...DEFAULT_DASHBOARD_INPUT,
description: `wow would you look at that? Wow.`,
},
});
const dashboard = await createDashboard(embeddableId, {}, 0, 'wow-such-id');
expect(
pluginServices.getServices().dashboardSavedObject.loadDashboardStateFromSavedObject
).toHaveBeenCalledWith({ id: 'wow-such-id' });
expect(dashboard.getState().explicitInput.description).toBe('wow would you look at that? Wow.');
expect(dashboard.getState().explicitInput.description).toBe(`wow would you look at that? Wow.`);
});

test('pulls state from session storage which overrides state from saved object', async () => {
pluginServices.getServices().dashboardSavedObject.loadDashboardStateFromSavedObject = jest
.fn()
.mockResolvedValue({ dashboardInput: { description: 'wow this description is okay' } });
.mockResolvedValue({
dashboardInput: {
...DEFAULT_DASHBOARD_INPUT,
description: 'wow this description is okay',
},
});
pluginServices.getServices().dashboardSessionStorage.getState = jest
.fn()
.mockReturnValue({ description: 'wow this description marginally better' });
Expand All @@ -83,7 +95,12 @@ test('pulls state from session storage which overrides state from saved object',
test('pulls state from creation options initial input which overrides all other state sources', async () => {
pluginServices.getServices().dashboardSavedObject.loadDashboardStateFromSavedObject = jest
.fn()
.mockResolvedValue({ dashboardInput: { description: 'wow this description is okay' } });
.mockResolvedValue({
dashboardInput: {
...DEFAULT_DASHBOARD_INPUT,
description: 'wow this description is okay',
},
});
pluginServices.getServices().dashboardSessionStorage.getState = jest
.fn()
.mockReturnValue({ description: 'wow this description marginally better' });
Expand Down Expand Up @@ -213,6 +230,7 @@ test('creates new embeddable with incoming embeddable if id does not match exist
},
},
});

// flush promises
await new Promise((r) => setTimeout(r, 1));
expect(mockContactCardFactory.create).toHaveBeenCalledWith(
Expand Down

0 comments on commit 5779773

Please sign in to comment.