Skip to content

Commit

Permalink
test: Add unit tests for StagingBanner - refs #253277
Browse files Browse the repository at this point in the history
  • Loading branch information
ana-oprea authored Jun 13, 2023
1 parent 92dae35 commit cf8c1a6
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions src/StagingBanner.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import React from 'react';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
import { render, waitFor } from '@testing-library/react';
import StagingBanner from './StagingBanner';
import { getBannerConfig } from '@eeacms/volto-banner/actions';
import '@testing-library/jest-dom/extend-expect';

const mockStore = configureStore([]);

jest.mock('@eeacms/volto-banner/actions', () => ({
getBannerConfig: jest.fn(),
}));

describe('StagingBanner', () => {
let store;

beforeEach(() => {
store = mockStore({
banner: {
config: {
static_banner: {
enabled: true,
visible_to_all: true,
title: 'Test Title',
message: 'Test Message',
},
dynamic_banner: {
enabled: true,
visible_to_all: true,
title: 'Dynamic Title',
message: 'Dynamic Message',
rancher_stacks_status: 'Dynamic Banner Status',
},
parentNodeSelector: '#testId',
bannerIcon: 'testIcon',
bannerCloseIcon: 'testCloseIcon',
},
},
actions: {
actions: {
object: {},
},
},
userSession: {
token: null,
},
});

store.dispatch = jest.fn();
});

it('dispatches getBannerConfig action on mount and does not display the banners', () => {
const store = mockStore({
banner: {
config: {
static_banner: {
enabled: false,
visible_to_all: true,
title: 'Test Title',
message: 'Test Message',
},
dynamic_banner: {
enabled: false,
visible_to_all: true,
title: 'Dynamic Title',
message: undefined,
rancher_stacks_status: 'Dynamic Banner Status',
},
parentNodeSelector: '#testId',
bannerIcon: 'testIcon',
bannerCloseIcon: 'testCloseIcon',
},
},
actions: {
actions: {
object: {},
},
},
userSession: {
token: null,
},
});

store.dispatch = jest.fn();
render(
<Provider store={store}>
<StagingBanner />
</Provider>,
);

expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(getBannerConfig());
});

it('renders static and dynamic banners when enabled', async () => {
const parentNode = document.createElement('div');
parentNode.setAttribute('id', 'testId');
document.body.appendChild(parentNode);

const { getByText } = render(
<Provider store={store}>
<StagingBanner />
</Provider>,
);

await waitFor(() => {
expect(getByText('Test Title')).toBeInTheDocument();
expect(getByText('Test Message')).toBeInTheDocument();
expect(getByText('Dynamic Title')).toBeInTheDocument();
expect(getByText('Dynamic Message')).toBeInTheDocument();
});
});

it('dispatches getBannerConfig action on mount', () => {
const store = mockStore({
banner: {
config: undefined,
},
actions: {
actions: {
object: {},
},
},
userSession: {
token: null,
},
});
store.dispatch = jest.fn();

render(
<Provider store={store}>
<StagingBanner />
</Provider>,
);

expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(getBannerConfig());
});
});

0 comments on commit cf8c1a6

Please sign in to comment.