|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { IntlProvider } from 'react-intl'; |
| 4 | +import { expect } from 'chai'; |
| 5 | +import { describe, it } from 'mocha'; |
| 6 | + |
| 7 | +import { ConfigProvider } from '../../app/configurations/ConfigContext'; |
| 8 | +import CrisisBannerHsl from '../../app/component/CrisisBannerHsl'; |
| 9 | + |
| 10 | +const primaryBanner = { body: 'Primary alert', priority: 'Primary' }; |
| 11 | +const secondaryBanner = { body: 'Secondary alert', priority: 'Secondary' }; |
| 12 | + |
| 13 | +const baseConfig = { |
| 14 | + CONFIG: 'hsl', |
| 15 | + URL: { BANNERS: null }, |
| 16 | +}; |
| 17 | + |
| 18 | +const renderWithBanners = (banners = []) => { |
| 19 | + const { container } = render( |
| 20 | + <IntlProvider locale="fi" messages={{}}> |
| 21 | + <ConfigProvider value={baseConfig}> |
| 22 | + <CrisisBannerHsl initialBanners={banners} /> |
| 23 | + </ConfigProvider> |
| 24 | + </IntlProvider>, |
| 25 | + ); |
| 26 | + return container; |
| 27 | +}; |
| 28 | + |
| 29 | +describe('<CrisisBannerHsl />', () => { |
| 30 | + it('renders nothing when no banners are provided', () => { |
| 31 | + const container = renderWithBanners([]); |
| 32 | + expect(container.firstChild).to.equal(null); |
| 33 | + }); |
| 34 | + |
| 35 | + it('renders the correct number of banners', () => { |
| 36 | + const container = renderWithBanners([primaryBanner, secondaryBanner]); |
| 37 | + expect( |
| 38 | + container.querySelectorAll('.crisis-banners-banner'), |
| 39 | + ).to.have.lengthOf(2); |
| 40 | + }); |
| 41 | + |
| 42 | + it('renders primary banner with the alert icon', () => { |
| 43 | + const container = renderWithBanners([primaryBanner]); |
| 44 | + expect( |
| 45 | + container.querySelector('.crisis-banners-banner-primary'), |
| 46 | + ).to.not.equal(null); |
| 47 | + expect( |
| 48 | + container.querySelector('.crisis-banners-banner-primary-icon'), |
| 49 | + ).to.not.equal(null); |
| 50 | + }); |
| 51 | + |
| 52 | + it('renders secondary banner without the alert icon', () => { |
| 53 | + const container = renderWithBanners([secondaryBanner]); |
| 54 | + expect( |
| 55 | + container.querySelector('.crisis-banners-banner-secondary'), |
| 56 | + ).to.not.equal(null); |
| 57 | + expect( |
| 58 | + container.querySelector('.crisis-banners-banner-primary-icon'), |
| 59 | + ).to.equal(null); |
| 60 | + }); |
| 61 | + |
| 62 | + it('renders banner body as HTML', () => { |
| 63 | + const container = renderWithBanners([ |
| 64 | + { body: '<b>Alert!</b>', priority: 'Secondary' }, |
| 65 | + ]); |
| 66 | + expect(container.querySelector('b')).to.not.equal(null); |
| 67 | + expect(container.querySelector('b').textContent).to.equal('Alert!'); |
| 68 | + }); |
| 69 | +}); |
0 commit comments