|
| 1 | +import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary'; |
| 2 | + |
| 3 | +import type {PathMappingValue} from 'sentry/components/connectRepository/pathMapping'; |
| 4 | +import {PathMappingList} from 'sentry/components/connectRepository/pathMappingList'; |
| 5 | + |
| 6 | +function renderPathMappingList( |
| 7 | + props: Partial<Parameters<typeof PathMappingList>[0]> = {} |
| 8 | +) { |
| 9 | + return render(<PathMappingList onChange={() => {}} {...props} />); |
| 10 | +} |
| 11 | + |
| 12 | +const MAPPINGS: PathMappingValue[] = [ |
| 13 | + {stackRoot: 'app/', sourceRoot: 'static/app/', branch: 'main'}, |
| 14 | + {stackRoot: 'src/', sourceRoot: 'src/app/', branch: 'frontend'}, |
| 15 | +]; |
| 16 | + |
| 17 | +describe('PathMappingList', () => { |
| 18 | + describe('empty', () => { |
| 19 | + it('starts with a single new mapping open for editing', () => { |
| 20 | + renderPathMappingList(); |
| 21 | + |
| 22 | + expect(screen.getByText(/Paths \(1\)/)).toBeInTheDocument(); |
| 23 | + expect(screen.getByRole('textbox', {name: 'Stack trace root'})).toBeInTheDocument(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('disables "Add another path" while the empty row is being edited', () => { |
| 27 | + renderPathMappingList(); |
| 28 | + |
| 29 | + expect(screen.getByRole('button', {name: 'Add another path'})).toBeDisabled(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('reports filled values through onChange', async () => { |
| 33 | + const onChange = jest.fn(); |
| 34 | + renderPathMappingList({onChange}); |
| 35 | + |
| 36 | + await userEvent.type( |
| 37 | + screen.getByRole('textbox', {name: 'Stack trace root'}), |
| 38 | + 'lib/' |
| 39 | + ); |
| 40 | + |
| 41 | + expect(onChange).toHaveBeenLastCalledWith([ |
| 42 | + expect.objectContaining({stackRoot: 'lib/', branch: 'main'}), |
| 43 | + ]); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('with existing mappings', () => { |
| 48 | + it('renders each mapping as a collapsed summary', () => { |
| 49 | + renderPathMappingList({pathMappings: MAPPINGS}); |
| 50 | + |
| 51 | + expect(screen.getByText(/Paths \(2\)/)).toBeInTheDocument(); |
| 52 | + expect(screen.getByText('app/')).toBeInTheDocument(); |
| 53 | + expect(screen.getByText('src/')).toBeInTheDocument(); |
| 54 | + expect( |
| 55 | + screen.queryByRole('textbox', {name: 'Stack trace root'}) |
| 56 | + ).not.toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('expands a single mapping at a time', async () => { |
| 60 | + renderPathMappingList({pathMappings: MAPPINGS}); |
| 61 | + |
| 62 | + const [first, second] = screen.getAllByRole('button', { |
| 63 | + name: 'Expand path mapping', |
| 64 | + }); |
| 65 | + |
| 66 | + await userEvent.click(first!); |
| 67 | + expect(screen.getByRole('textbox', {name: 'Stack trace root'})).toHaveValue('app/'); |
| 68 | + |
| 69 | + await userEvent.click(second!); |
| 70 | + expect(screen.getByRole('textbox', {name: 'Stack trace root'})).toHaveValue('src/'); |
| 71 | + expect(screen.getAllByRole('textbox', {name: 'Stack trace root'})).toHaveLength(1); |
| 72 | + }); |
| 73 | + |
| 74 | + it('adds a new mapping when "Add another path" is clicked', async () => { |
| 75 | + renderPathMappingList({pathMappings: MAPPINGS}); |
| 76 | + |
| 77 | + await userEvent.click(screen.getByRole('button', {name: 'Add another path'})); |
| 78 | + |
| 79 | + expect(screen.getByText(/Paths \(3\)/)).toBeInTheDocument(); |
| 80 | + expect(screen.getByRole('textbox', {name: 'Stack trace root'})).toHaveValue(''); |
| 81 | + }); |
| 82 | + |
| 83 | + it('removes a mapping and reports the change', async () => { |
| 84 | + const onChange = jest.fn(); |
| 85 | + renderPathMappingList({pathMappings: MAPPINGS, onChange}); |
| 86 | + |
| 87 | + const [firstDelete] = screen.getAllByRole('button', { |
| 88 | + name: 'Delete path mapping', |
| 89 | + }); |
| 90 | + await userEvent.click(firstDelete!); |
| 91 | + |
| 92 | + expect(screen.getByText(/Paths \(1\)/)).toBeInTheDocument(); |
| 93 | + expect(onChange).toHaveBeenLastCalledWith([ |
| 94 | + expect.objectContaining({stackRoot: 'src/'}), |
| 95 | + ]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('falls back to a fresh open row when the last mapping is deleted', async () => { |
| 99 | + const onChange = jest.fn(); |
| 100 | + renderPathMappingList({pathMappings: [MAPPINGS[0]!], onChange}); |
| 101 | + |
| 102 | + await userEvent.click(screen.getByRole('button', {name: 'Delete path mapping'})); |
| 103 | + |
| 104 | + expect(screen.getByText(/Paths \(1\)/)).toBeInTheDocument(); |
| 105 | + expect(screen.getByRole('textbox', {name: 'Stack trace root'})).toHaveValue(''); |
| 106 | + expect(onChange).toHaveBeenLastCalledWith([]); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('duplicate mappings', () => { |
| 111 | + it('blocks adding another path until the duplicate is resolved', () => { |
| 112 | + const duplicates: PathMappingValue[] = [ |
| 113 | + {stackRoot: 'app/', sourceRoot: 'static/app/', branch: 'main'}, |
| 114 | + {stackRoot: 'app/', sourceRoot: 'static/app/', branch: 'main'}, |
| 115 | + ]; |
| 116 | + renderPathMappingList({pathMappings: duplicates}); |
| 117 | + |
| 118 | + expect(screen.getByRole('button', {name: 'Add another path'})).toBeDisabled(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('treats an empty branch as a duplicate of the default branch', () => { |
| 122 | + const duplicates: PathMappingValue[] = [ |
| 123 | + {stackRoot: 'app/', sourceRoot: 'static/app/', branch: ''}, |
| 124 | + {stackRoot: 'app/', sourceRoot: 'static/app/', branch: 'main'}, |
| 125 | + ]; |
| 126 | + renderPathMappingList({pathMappings: duplicates}); |
| 127 | + |
| 128 | + expect(screen.getByRole('button', {name: 'Add another path'})).toBeDisabled(); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('add another path', () => { |
| 133 | + it('reopens a trailing empty row instead of stacking a new one', async () => { |
| 134 | + renderPathMappingList({pathMappings: [MAPPINGS[0]!]}); |
| 135 | + |
| 136 | + // Add a new empty row, then collapse it by expanding the existing one. |
| 137 | + await userEvent.click(screen.getByRole('button', {name: 'Add another path'})); |
| 138 | + expect(screen.getByText(/Paths \(2\)/)).toBeInTheDocument(); |
| 139 | + |
| 140 | + await userEvent.click(screen.getByRole('button', {name: 'Expand path mapping'})); |
| 141 | + expect(screen.getByRole('textbox', {name: 'Stack trace root'})).toHaveValue('app/'); |
| 142 | + |
| 143 | + // The trailing empty row is now collapsed, so adding reopens it rather |
| 144 | + // than appending a third row. |
| 145 | + await userEvent.click(screen.getByRole('button', {name: 'Add another path'})); |
| 146 | + |
| 147 | + expect(screen.getByText(/Paths \(2\)/)).toBeInTheDocument(); |
| 148 | + expect(screen.getByRole('textbox', {name: 'Stack trace root'})).toHaveValue(''); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments