Skip to content

Commit

Permalink
=added tests config
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Nov 19, 2024
1 parent 396c29c commit 6cd8096
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
18 changes: 18 additions & 0 deletions examples/grid_example/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../..',
roots: ['<rootDir>/examples/grid_example'],
coverageDirectory: '<rootDir>/target/kibana-coverage/jest/examples/grid_example',
coverageReporters: ['text', 'html'],
collectCoverageFrom: ['<rootDir>/examples/grid_example/{common,public,server}/**/*.{ts,tsx}'],
setupFiles: ['jest-canvas-mock'],
};
32 changes: 32 additions & 0 deletions examples/grid_example/public/app.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import React from 'react';
import { GridExample } from './app';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { CoreStart } from '@kbn/core-lifecycle-browser';
import { initialGridLayout } from './serialized_grid_layout';

describe('GridExample', () => {
let coreStart: CoreStart;

beforeEach(() => {
coreStart = {} as CoreStart;
});

it('renders all the panels', () => {
render(<GridExample coreStart={coreStart} />);
const panelTitles = screen
.getAllByTestId('gridExamplePanelTitle')
.map((panel) => panel.textContent);
const expectedPanelTitles = initialGridLayout.flatMap(({ panels }) => Object.keys(panels));
expect(panelTitles).toEqual(expectedPanelTitles);
});
});
10 changes: 8 additions & 2 deletions examples/grid_example/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const GridExample = ({ coreStart }: { coreStart: CoreStart }) => {
<EuiFlexItem grow={false}>
<EuiButton
onClick={async () => {
setExpandedPanelId(undefined);
const panelId = await getPanelId({
coreStart,
suggestion: `panel${(gridLayoutApi?.getPanelCount() ?? 0) + 1}`,
Expand Down Expand Up @@ -156,10 +157,13 @@ export const GridExample = ({ coreStart }: { coreStart: CoreStart }) => {
renderPanelContents={(id) => {
return (
<>
<div style={{ padding: 8 }}>{id}</div>
<div style={{ padding: 8 }} data-test-subj="gridExamplePanelTitle">
{id}
</div>
<EuiButtonEmpty
onClick={() => {
gridLayoutApi?.removePanel(id);
setExpandedPanelId(undefined);
}}
>
{i18n.translate('examples.gridExample.deletePanelButton', {
Expand All @@ -173,6 +177,7 @@ export const GridExample = ({ coreStart }: { coreStart: CoreStart }) => {
suggestion: `panel${(gridLayoutApi?.getPanelCount() ?? 0) + 1}`,
});
if (newPanelId) gridLayoutApi?.replacePanel(id, newPanelId);
setExpandedPanelId(undefined);
}}
>
{i18n.translate('examples.gridExample.replacePanelButton', {
Expand All @@ -185,7 +190,8 @@ export const GridExample = ({ coreStart }: { coreStart: CoreStart }) => {
setExpandedPanelId((expandedId) => (expandedId ? undefined : id))
}
aria-label={i18n.translate('examples.gridExample.maximizePanel', {
defaultMessage: 'Maximize/minimize panel',
defaultMessage: 'Maximize/minimize panel {id}',
values: { id },
})}
/>
</>
Expand Down
2 changes: 1 addition & 1 deletion examples/grid_example/public/serialized_grid_layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function setSerializedGridLayout(layout: GridLayoutData) {
sessionStorage.setItem(STATE_SESSION_STORAGE_KEY, JSON.stringify(layout));
}

const initialGridLayout: GridLayoutData = [
export const initialGridLayout: GridLayoutData = [
{
title: 'Large section',
isCollapsed: false,
Expand Down

0 comments on commit 6cd8096

Please sign in to comment.