import userEvent from '@testing-library/user-event' import React from 'react' import renderWithTheme from '../../tests/helpers/renderWithTheme' import Alert from '../Alert/Alert' import Notification from './Notification' import NotificationProvider, { AlertArgs, ElementArgs, useNotifications } from './Provider' describe('component: Notification', () => { test('should render children in the correct position on the page', () => { const { getByTestId, rerender } = renderWithTheme( Error Notification ) let wrapper = getByTestId('notification-wrapper') expect(wrapper).toHaveStyle('top: 40px') expect(wrapper).toHaveStyle('left: 40px') rerender( Error Notification ) wrapper = getByTestId('notification-wrapper') expect(wrapper).toHaveStyle('top: 40px') expect(wrapper).toHaveStyle('left: 50%') expect(wrapper).toHaveStyle('transform: translateX(-50%)') rerender( Error Notification ) wrapper = getByTestId('notification-wrapper') expect(wrapper).toHaveStyle('top: 40px') expect(wrapper).toHaveStyle('right: 40px') rerender( Error Notification ) wrapper = getByTestId('notification-wrapper') expect(wrapper).toHaveStyle('bottom: 40px') expect(wrapper).toHaveStyle('left: 40px') rerender( Error Notification ) wrapper = getByTestId('notification-wrapper') expect(wrapper).toHaveStyle('bottom: 40px') expect(wrapper).toHaveStyle('left: 50%') expect(wrapper).toHaveStyle('transform: translateX(-50%)') rerender( Error Notification ) wrapper = getByTestId('notification-wrapper') expect(wrapper).toHaveStyle('bottom: 40px') expect(wrapper).toHaveStyle('right: 40px') }) test('should not render children when open is false', () => { const { queryByText } = renderWithTheme( Error Notification ) expect(queryByText('Error Notification')).toBeNull() }) }) describe('component: NotificationProvider', () => { function NotificationSetter(args: AlertArgs): null function NotificationSetter(args: ElementArgs): null function NotificationSetter(args: any) { const { setNotification, clearNotification } = useNotifications() React.useEffect(() => { const key = setNotification(args) return () => clearNotification(key) }) return null } test('should render single notification per position', () => { const { getByLabelText, getByTestId } = renderWithTheme( ) const br1 = getByLabelText('br1') const br2 = getByTestId('br2') const tl1 = getByLabelText('tl1') const bc1 = getByTestId('bc1') expect(br1).toHaveTextContent('Hey') expect(br2).toHaveTextContent('Ho') expect(tl1).toHaveTextContent('Where') expect(bc1).toHaveTextContent('You Go') expect(br1.parentElement).toBe(br2.parentElement) expect(br1.parentElement).not.toBe(tl1.parentElement) expect(br1.parentElement).not.toBe(bc1.parentElement) expect(bc1.parentElement).not.toBe(tl1.parentElement) expect(br1.parentElement).toHaveStyle({ bottom: '40px', right: '40px' }) expect(tl1.parentElement).toHaveStyle({ top: '40px', left: '40px' }) expect(bc1.parentElement).toHaveStyle({ bottom: '40px', left: '50%', transform: 'translateX(-50%)', }) }) test('should render custom element', () => { const ref = React.createRef() const { getByTestId } = renderWithTheme( } /> ) expect(getByTestId('t123')).toBe(ref.current) expect(ref.current?.parentElement).toHaveStyle({ bottom: '40px', right: '40px' }) }) test('should remove notification on close', () => { const { getByLabelText } = renderWithTheme( emphasis} status="info" canClose closeLabel="So it closes" aria-label="alert" /> ) const notification = getByLabelText('alert').parentElement expect(notification).toHaveTextContent('emphasis') userEvent.click(getByLabelText('So it closes')) expect(notification).not.toBeInTheDocument() }) })