|
| 1 | +import { fireEvent, screen } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import { NihDatasetPermission } from 'src/libs/ajax/User'; |
| 4 | +import { renderWithAppContexts as render } from 'src/testing/test-utils'; |
| 5 | + |
| 6 | +import { NihResources } from './NihResources'; |
| 7 | + |
| 8 | +const mockAuthorized: NihDatasetPermission[] = [ |
| 9 | + { name: 'Dataset A', authorized: true }, |
| 10 | + { name: 'Dataset B', authorized: true }, |
| 11 | +]; |
| 12 | + |
| 13 | +const mockUnauthorized: NihDatasetPermission[] = [ |
| 14 | + { name: 'Dataset C', authorized: false }, |
| 15 | + { name: 'Dataset D', authorized: false }, |
| 16 | +]; |
| 17 | + |
| 18 | +describe('NihResources', () => { |
| 19 | + describe('Component Structure', () => { |
| 20 | + it('should display the Resources header', () => { |
| 21 | + // Arrange |
| 22 | + const props = { authorizedDatasets: [], unauthorizedDatasets: [] }; |
| 23 | + |
| 24 | + // Act |
| 25 | + render(<NihResources {...props} />); |
| 26 | + |
| 27 | + // Assert |
| 28 | + expect(screen.getByText('Resources')).toBeInTheDocument(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('Authorized Datasets Section', () => { |
| 33 | + it('should display authorized datasets section when datasets are provided', () => { |
| 34 | + // Arrange |
| 35 | + const props = { authorizedDatasets: mockAuthorized, unauthorizedDatasets: [] }; |
| 36 | + |
| 37 | + // Act |
| 38 | + render(<NihResources {...props} />); |
| 39 | + |
| 40 | + // Assert |
| 41 | + expect(screen.getByText('Authorized to access')).toBeInTheDocument(); |
| 42 | + mockAuthorized.forEach(({ name }) => { |
| 43 | + expect(screen.getByText(name)).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should not display authorized datasets section when no datasets are provided', () => { |
| 48 | + // Arrange |
| 49 | + const props = { authorizedDatasets: [], unauthorizedDatasets: [] }; |
| 50 | + |
| 51 | + // Act |
| 52 | + render(<NihResources {...props} />); |
| 53 | + |
| 54 | + // Assert |
| 55 | + expect(screen.queryByText('Authorized to access')).not.toBeInTheDocument(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('Unauthorized Datasets Section', () => { |
| 60 | + it('should display unauthorized datasets section header but hide content by default', () => { |
| 61 | + // Arrange |
| 62 | + const props = { authorizedDatasets: [], unauthorizedDatasets: mockUnauthorized }; |
| 63 | + |
| 64 | + // Act |
| 65 | + render(<NihResources {...props} />); |
| 66 | + |
| 67 | + // Assert |
| 68 | + expect(screen.getByText('Not authorized')).toBeInTheDocument(); |
| 69 | + mockUnauthorized.forEach(({ name }) => { |
| 70 | + expect(screen.queryByText(name)).not.toBeInTheDocument(); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should reveal unauthorized datasets when section header is clicked', () => { |
| 75 | + // Arrange |
| 76 | + const props = { authorizedDatasets: [], unauthorizedDatasets: mockUnauthorized }; |
| 77 | + render(<NihResources {...props} />); |
| 78 | + |
| 79 | + // Act |
| 80 | + fireEvent.click(screen.getByText('Not authorized')); |
| 81 | + |
| 82 | + // Assert |
| 83 | + mockUnauthorized.forEach(({ name }) => { |
| 84 | + expect(screen.getByText(name)).toBeInTheDocument(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not display unauthorized datasets section when no datasets are provided', () => { |
| 89 | + // Arrange |
| 90 | + const props = { authorizedDatasets: [], unauthorizedDatasets: [] }; |
| 91 | + |
| 92 | + // Act |
| 93 | + render(<NihResources {...props} />); |
| 94 | + |
| 95 | + // Assert |
| 96 | + expect(screen.queryByText('Not authorized')).not.toBeInTheDocument(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('Combined Sections Behavior', () => { |
| 101 | + it('should display both sections independently with correct initial states', () => { |
| 102 | + // Arrange |
| 103 | + const props = { authorizedDatasets: mockAuthorized, unauthorizedDatasets: mockUnauthorized }; |
| 104 | + |
| 105 | + // Act |
| 106 | + render(<NihResources {...props} />); |
| 107 | + |
| 108 | + // Assert - the Authorized section is expanded by default |
| 109 | + expect(screen.getByText('Authorized to access')).toBeInTheDocument(); |
| 110 | + mockAuthorized.forEach(({ name }) => { |
| 111 | + expect(screen.getByText(name)).toBeInTheDocument(); |
| 112 | + }); |
| 113 | + |
| 114 | + // Assert - Unauthorized section is collapsed by default |
| 115 | + expect(screen.getByText('Not authorized')).toBeInTheDocument(); |
| 116 | + mockUnauthorized.forEach(({ name }) => { |
| 117 | + expect(screen.queryByText(name)).not.toBeInTheDocument(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should toggle unauthorized section independently of authorized section', () => { |
| 122 | + // Arrange |
| 123 | + const props = { authorizedDatasets: mockAuthorized, unauthorizedDatasets: mockUnauthorized }; |
| 124 | + render(<NihResources {...props} />); |
| 125 | + |
| 126 | + // Act |
| 127 | + fireEvent.click(screen.getByText('Not authorized')); |
| 128 | + |
| 129 | + // Assert - the Authorized section remains visible |
| 130 | + mockAuthorized.forEach(({ name }) => { |
| 131 | + expect(screen.getByText(name)).toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + // Assert - the Unauthorized section is now visible |
| 135 | + mockUnauthorized.forEach(({ name }) => { |
| 136 | + expect(screen.getByText(name)).toBeInTheDocument(); |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('Empty State', () => { |
| 142 | + it('should only display header when no datasets are provided', () => { |
| 143 | + // Arrange |
| 144 | + const props = { authorizedDatasets: [], unauthorizedDatasets: [] }; |
| 145 | + |
| 146 | + // Act |
| 147 | + render(<NihResources {...props} />); |
| 148 | + |
| 149 | + // Assert |
| 150 | + expect(screen.getByText('Resources')).toBeInTheDocument(); |
| 151 | + expect(screen.queryByText('Authorized to access')).not.toBeInTheDocument(); |
| 152 | + expect(screen.queryByText('Not authorized')).not.toBeInTheDocument(); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments