|
| 1 | +/* eslint-disable max-classes-per-file */ |
| 2 | +import React from 'react'; |
| 3 | +import { renderHook } from '@testing-library/react-hooks'; |
| 4 | +import usePanelRegistration from './usePanelRegistration'; |
| 5 | +import { PanelProps } from '../DashboardPlugin'; |
| 6 | + |
| 7 | +/* eslint-disable react/prefer-stateless-function */ |
| 8 | +class ClassCOMPONENT extends React.Component<PanelProps> { |
| 9 | + static COMPONENT = 'ClassCOMPONENT'; |
| 10 | +} |
| 11 | +class ClassDisplayName extends React.Component<PanelProps> { |
| 12 | + static displayName = 'ClassDisplayName'; |
| 13 | +} |
| 14 | +class ClassNoName extends React.Component<PanelProps> {} |
| 15 | +/* eslint-enable react/prefer-stateless-function */ |
| 16 | + |
| 17 | +function FnCOMPONENT() { |
| 18 | + return null; |
| 19 | +} |
| 20 | +FnCOMPONENT.COMPONENT = 'FnCOMPONENT'; |
| 21 | +function FnDisplayName() { |
| 22 | + return null; |
| 23 | +} |
| 24 | +FnDisplayName.displayName = 'FnDisplayName'; |
| 25 | +function FnNoName() { |
| 26 | + return null; |
| 27 | +} |
| 28 | + |
| 29 | +const deregister = jest.fn(); |
| 30 | +const registerComponent = jest.fn(); |
| 31 | +const hydrate = jest.fn(); |
| 32 | +const dehydrate = jest.fn(); |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + registerComponent.mockReturnValue(deregister); |
| 37 | +}); |
| 38 | + |
| 39 | +it.each([ |
| 40 | + [ClassCOMPONENT.COMPONENT, ClassCOMPONENT], |
| 41 | + [ClassDisplayName.displayName, ClassDisplayName], |
| 42 | + [FnCOMPONENT.COMPONENT, FnCOMPONENT], |
| 43 | + [FnDisplayName.displayName, FnDisplayName], |
| 44 | +])( |
| 45 | + 'should register components with COMPONENT or displayName attributes and deregister on unmount: "%s"', |
| 46 | + (_label, ComponentType) => { |
| 47 | + const { unmount } = renderHook(() => |
| 48 | + usePanelRegistration(registerComponent, ComponentType, hydrate, dehydrate) |
| 49 | + ); |
| 50 | + |
| 51 | + const name = |
| 52 | + 'COMPONENT' in ComponentType |
| 53 | + ? ComponentType.COMPONENT |
| 54 | + : ComponentType.displayName; |
| 55 | + |
| 56 | + expect(registerComponent).toHaveBeenCalledWith( |
| 57 | + name, |
| 58 | + ComponentType, |
| 59 | + hydrate, |
| 60 | + dehydrate |
| 61 | + ); |
| 62 | + |
| 63 | + expect(deregister).not.toHaveBeenCalled(); |
| 64 | + |
| 65 | + unmount(); |
| 66 | + |
| 67 | + expect(deregister).toHaveBeenCalled(); |
| 68 | + } |
| 69 | +); |
| 70 | + |
| 71 | +it.each([ |
| 72 | + ['MockClassNoName', ClassNoName], |
| 73 | + ['MockFnNoName', FnNoName], |
| 74 | +])( |
| 75 | + 'should throw an error if no COMPONENT or displayName attribute exists: "%s"', |
| 76 | + (_label, ComponentType) => { |
| 77 | + const { result } = renderHook(() => |
| 78 | + usePanelRegistration(registerComponent, ComponentType, hydrate, dehydrate) |
| 79 | + ); |
| 80 | + |
| 81 | + expect(result.error).toEqual( |
| 82 | + new Error( |
| 83 | + 'ComponentType must have a `COMPONENT` or `displayName` attribute.' |
| 84 | + ) |
| 85 | + ); |
| 86 | + } |
| 87 | +); |
0 commit comments