|
1 | 1 | import React from 'react'; |
2 | | -import { fireEvent, screen } from '@testing-library/react'; |
| 2 | +import { act, fireEvent, screen } from '@testing-library/react'; |
3 | 3 | import type { MultichainNetworkConfiguration } from '@metamask/multichain-network-controller'; |
4 | 4 | import { renderWithProvider } from '../../../../test/lib/render-helpers-navigate'; |
5 | 5 | import configureStore from '../../../store/store'; |
@@ -99,7 +99,10 @@ describe('NetworkPillSelector', () => { |
99 | 99 | renderSelector(); |
100 | 100 |
|
101 | 101 | fireEvent.click(screen.getByTestId('network-pill-0xa')); |
102 | | - expect(mockHandleSelectNetwork).toHaveBeenCalledWith('eip155:10'); |
| 102 | + expect(mockHandleSelectNetwork).toHaveBeenCalledWith( |
| 103 | + 'eip155:10', |
| 104 | + 'Network Pill', |
| 105 | + ); |
103 | 106 |
|
104 | 107 | fireEvent.click(screen.getByTestId('network-pill-all-default')); |
105 | 108 | expect(mockHandleSelectAll).toHaveBeenCalledTimes(1); |
@@ -138,23 +141,113 @@ describe('NetworkPillSelector', () => { |
138 | 141 | }); |
139 | 142 |
|
140 | 143 | describe('when pills wrap onto more than one line', () => { |
| 144 | + afterEach(() => { |
| 145 | + jest.restoreAllMocks(); |
| 146 | + }); |
| 147 | + |
141 | 148 | it('toggles between the collapsed and expanded view', () => { |
142 | | - // The Optimism pill sits on a second line |
| 149 | + // Three 100px pills in a 250px row, so the last one wraps. The toggle |
| 150 | + // is 40px and still fits beside the first two. |
143 | 151 | jest |
144 | | - .spyOn(HTMLElement.prototype, 'offsetTop', 'get') |
| 152 | + .spyOn(HTMLElement.prototype, 'clientWidth', 'get') |
| 153 | + .mockReturnValue(250); |
| 154 | + jest |
| 155 | + .spyOn(HTMLElement.prototype, 'offsetWidth', 'get') |
145 | 156 | .mockImplementation(function mock(this: HTMLElement) { |
146 | | - return this.dataset.testid === 'network-pill-0xa' ? 56 : 0; |
| 157 | + if (this.dataset.testid === 'network-pill-toggle') { |
| 158 | + return 40; |
| 159 | + } |
| 160 | + if (this.dataset.testid?.startsWith('network-pill')) { |
| 161 | + return 100; |
| 162 | + } |
| 163 | + return 0; |
147 | 164 | }); |
148 | 165 |
|
149 | 166 | renderSelector(); |
150 | 167 | const toggle = screen.getByTestId('network-pill-toggle'); |
151 | | - expect(toggle).toHaveTextContent('Show 1 more networks'); |
| 168 | + const pills = screen.getByTestId( |
| 169 | + 'network-pill-selector', |
| 170 | + ).firstElementChild; |
| 171 | + expect(toggle).toHaveTextContent('Show all (3)'); |
152 | 172 |
|
153 | 173 | fireEvent.click(toggle); |
154 | | - expect(toggle).toHaveTextContent('Show fewer networks'); |
| 174 | + const expandedToggle = screen.getByTestId('network-pill-toggle'); |
| 175 | + expect(expandedToggle).toHaveTextContent('Show fewer'); |
155 | 176 |
|
156 | | - fireEvent.click(toggle); |
157 | | - expect(toggle).toHaveTextContent('Show 1 more networks'); |
| 177 | + fireEvent.click(expandedToggle); |
| 178 | + const collapsedToggle = screen.getByTestId('network-pill-toggle'); |
| 179 | + expect(collapsedToggle).toHaveTextContent('Show all (3)'); |
| 180 | + }); |
| 181 | + |
| 182 | + it('remeasures when a pill changes width', () => { |
| 183 | + let notify: ResizeObserverCallback | undefined; |
| 184 | + const observed: Element[] = []; |
| 185 | + const originalResizeObserver = window.ResizeObserver; |
| 186 | + window.ResizeObserver = class { |
| 187 | + constructor(callback: ResizeObserverCallback) { |
| 188 | + notify = callback; |
| 189 | + } |
| 190 | + |
| 191 | + observe(target: Element) { |
| 192 | + observed.push(target); |
| 193 | + } |
| 194 | + |
| 195 | + unobserve() { |
| 196 | + // no-op |
| 197 | + } |
| 198 | + |
| 199 | + disconnect() { |
| 200 | + // no-op |
| 201 | + } |
| 202 | + } as typeof ResizeObserver; |
| 203 | + |
| 204 | + jest |
| 205 | + .spyOn(HTMLElement.prototype, 'clientWidth', 'get') |
| 206 | + .mockReturnValue(400); |
| 207 | + const offsetWidth = jest |
| 208 | + .spyOn(HTMLElement.prototype, 'offsetWidth', 'get') |
| 209 | + .mockImplementation(function mock(this: HTMLElement) { |
| 210 | + if (this.dataset.testid === 'network-pill-toggle') { |
| 211 | + return 40; |
| 212 | + } |
| 213 | + if (this.dataset.testid?.startsWith('network-pill')) { |
| 214 | + return 100; |
| 215 | + } |
| 216 | + return 0; |
| 217 | + }); |
| 218 | + |
| 219 | + try { |
| 220 | + renderSelector(); |
| 221 | + expect( |
| 222 | + screen.queryByTestId('network-pill-toggle'), |
| 223 | + ).not.toBeInTheDocument(); |
| 224 | + expect( |
| 225 | + observed.some( |
| 226 | + (element) => |
| 227 | + element.getAttribute('data-testid') === 'network-pill-0x1', |
| 228 | + ), |
| 229 | + ).toBe(true); |
| 230 | + |
| 231 | + offsetWidth.mockImplementation(function mock(this: HTMLElement) { |
| 232 | + if (this.dataset.testid === 'network-pill-toggle') { |
| 233 | + return 40; |
| 234 | + } |
| 235 | + if (this.dataset.testid?.startsWith('network-pill')) { |
| 236 | + return 200; |
| 237 | + } |
| 238 | + return 0; |
| 239 | + }); |
| 240 | + act(() => { |
| 241 | + notify?.([], {} as ResizeObserver); |
| 242 | + }); |
| 243 | + |
| 244 | + expect(screen.getByTestId('network-pill-toggle')).toHaveTextContent( |
| 245 | + 'Show all (3)', |
| 246 | + ); |
| 247 | + expect(screen.getByTestId('network-pill-0xa')).toHaveAttribute('inert'); |
| 248 | + } finally { |
| 249 | + window.ResizeObserver = originalResizeObserver; |
| 250 | + } |
158 | 251 | }); |
159 | 252 | }); |
160 | 253 |
|
|
0 commit comments