|
| 1 | +import '@testing-library/jest-dom' |
| 2 | +import React from 'react' |
1 | 3 | import { Theme } from '@material-ui/core'
|
| 4 | +import { render } from '@testing-library/react' |
2 | 5 |
|
| 6 | +import { WebviewIntentProvider, WebviewService } from 'cozy-intent' |
| 7 | + |
| 8 | +import Dialog from '.' |
| 9 | +import { BreakpointsProvider } from '../hooks/useBreakpoints' |
3 | 10 | import { DOMStrings, makeOnMount, makeOnUnmount } from './DialogEffects'
|
4 | 11 | import { ThemeColor } from '../hooks/useSetFlagshipUi/useSetFlagshipUI'
|
5 | 12 |
|
@@ -219,3 +226,240 @@ it('should provide the inversed UI when Cozybar is not black on white, but white
|
219 | 226 | topTheme: ThemeColor.Light
|
220 | 227 | })
|
221 | 228 | })
|
| 229 | + |
| 230 | +jest.mock('cozy-device-helper', () => ({ |
| 231 | + isFlagshipApp: (): boolean => true, |
| 232 | + getFlagshipMetadata: (): Record<string, never> => ({}) |
| 233 | +})) |
| 234 | + |
| 235 | +const onOpenMountExpected = { |
| 236 | + bottomBackground: '#fff', |
| 237 | + bottomOverlay: 'rgba(0, 0, 0, 0.5)', |
| 238 | + bottomTheme: 'light', |
| 239 | + topOverlay: 'rgba(0, 0, 0, 0.5)', |
| 240 | + topTheme: 'light' |
| 241 | +} |
| 242 | + |
| 243 | +const onOpenUnmountExpected = { |
| 244 | + bottomBackground: '#fff', |
| 245 | + bottomOverlay: 'transparent', |
| 246 | + bottomTheme: 'dark', |
| 247 | + topOverlay: 'transparent', |
| 248 | + topTheme: 'dark' |
| 249 | +} |
| 250 | + |
| 251 | +it('should emit onMount() immediately and onUnmount() when the whole tree is deleted', () => { |
| 252 | + const caller = jest.fn<void, unknown[]>() |
| 253 | + const service = { |
| 254 | + call: (...args: unknown[]): void => caller(...args) |
| 255 | + } as WebviewService |
| 256 | + |
| 257 | + const { unmount } = render( |
| 258 | + <WebviewIntentProvider webviewService={service}> |
| 259 | + <BreakpointsProvider> |
| 260 | + <Dialog open /> |
| 261 | + </BreakpointsProvider> |
| 262 | + </WebviewIntentProvider> |
| 263 | + ) |
| 264 | + |
| 265 | + expect(caller).toHaveBeenNthCalledWith( |
| 266 | + 1, |
| 267 | + 'setFlagshipUI', |
| 268 | + onOpenMountExpected, |
| 269 | + 'cozy-ui/Dialog (onOpenMount)' |
| 270 | + ) |
| 271 | + |
| 272 | + unmount() |
| 273 | + |
| 274 | + expect(caller).toHaveBeenNthCalledWith( |
| 275 | + 2, |
| 276 | + 'setFlagshipUI', |
| 277 | + onOpenUnmountExpected, |
| 278 | + 'cozy-ui/Dialog (onOpenUnmount)' |
| 279 | + ) |
| 280 | +}) |
| 281 | + |
| 282 | +it('should emit onMount() immediately and onUnmount() when Dialog is deleted from the tree', () => { |
| 283 | + const caller = jest.fn<void, unknown[]>() |
| 284 | + const service = { |
| 285 | + call: (...args: unknown[]): void => caller(...args) |
| 286 | + } as WebviewService |
| 287 | + |
| 288 | + const { rerender } = render( |
| 289 | + <WebviewIntentProvider webviewService={service}> |
| 290 | + <BreakpointsProvider> |
| 291 | + <Dialog open /> |
| 292 | + </BreakpointsProvider> |
| 293 | + </WebviewIntentProvider> |
| 294 | + ) |
| 295 | + |
| 296 | + expect(caller).toHaveBeenNthCalledWith( |
| 297 | + 1, |
| 298 | + 'setFlagshipUI', |
| 299 | + onOpenMountExpected, |
| 300 | + 'cozy-ui/Dialog (onOpenMount)' |
| 301 | + ) |
| 302 | + |
| 303 | + rerender( |
| 304 | + <WebviewIntentProvider webviewService={service}></WebviewIntentProvider> |
| 305 | + ) |
| 306 | + |
| 307 | + expect(caller).toHaveBeenNthCalledWith( |
| 308 | + 2, |
| 309 | + 'setFlagshipUI', |
| 310 | + onOpenUnmountExpected, |
| 311 | + 'cozy-ui/Dialog (onOpenUnmount)' |
| 312 | + ) |
| 313 | +}) |
| 314 | + |
| 315 | +it('should not emit onMount() if mounted as open:false, then emit onMount() on open:true, then emit onUnmount() on switch back to open:false, then emit onMount() again on switch back to open:true, then emit onUnmount() again when the tree is deleted', () => { |
| 316 | + const caller = jest.fn<void, unknown[]>() |
| 317 | + const service = { |
| 318 | + call: (...args: unknown[]): void => caller(...args) |
| 319 | + } as WebviewService |
| 320 | + |
| 321 | + const { rerender, unmount } = render( |
| 322 | + <WebviewIntentProvider webviewService={service}> |
| 323 | + <BreakpointsProvider> |
| 324 | + <Dialog open={false} /> |
| 325 | + </BreakpointsProvider> |
| 326 | + </WebviewIntentProvider> |
| 327 | + ) |
| 328 | + |
| 329 | + expect(caller).not.toHaveBeenCalled() |
| 330 | + |
| 331 | + rerender( |
| 332 | + <WebviewIntentProvider webviewService={service}> |
| 333 | + <BreakpointsProvider> |
| 334 | + <Dialog open={true} /> |
| 335 | + </BreakpointsProvider> |
| 336 | + </WebviewIntentProvider> |
| 337 | + ) |
| 338 | + |
| 339 | + expect(caller).toHaveBeenNthCalledWith( |
| 340 | + 1, |
| 341 | + 'setFlagshipUI', |
| 342 | + onOpenMountExpected, |
| 343 | + 'cozy-ui/Dialog (onOpenMount)' |
| 344 | + ) |
| 345 | + |
| 346 | + rerender( |
| 347 | + <WebviewIntentProvider webviewService={service}> |
| 348 | + <BreakpointsProvider> |
| 349 | + <Dialog open={false} /> |
| 350 | + </BreakpointsProvider> |
| 351 | + </WebviewIntentProvider> |
| 352 | + ) |
| 353 | + |
| 354 | + expect(caller).toHaveBeenNthCalledWith( |
| 355 | + 2, |
| 356 | + 'setFlagshipUI', |
| 357 | + onOpenUnmountExpected, |
| 358 | + 'cozy-ui/Dialog (onOpenUnmount)' |
| 359 | + ) |
| 360 | + |
| 361 | + rerender( |
| 362 | + <WebviewIntentProvider webviewService={service}> |
| 363 | + <BreakpointsProvider> |
| 364 | + <Dialog open={true} /> |
| 365 | + </BreakpointsProvider> |
| 366 | + </WebviewIntentProvider> |
| 367 | + ) |
| 368 | + |
| 369 | + expect(caller).toHaveBeenNthCalledWith( |
| 370 | + 3, |
| 371 | + 'setFlagshipUI', |
| 372 | + onOpenMountExpected, |
| 373 | + 'cozy-ui/Dialog (onOpenMount)' |
| 374 | + ) |
| 375 | + |
| 376 | + unmount() |
| 377 | + |
| 378 | + expect(caller).toHaveBeenNthCalledWith( |
| 379 | + 4, |
| 380 | + 'setFlagshipUI', |
| 381 | + onOpenUnmountExpected, |
| 382 | + 'cozy-ui/Dialog (onOpenUnmount)' |
| 383 | + ) |
| 384 | +}) |
| 385 | + |
| 386 | +it('when provided with a faulty <Dialog /> that has no open prop, it should emit nothing at mount or unmount', () => { |
| 387 | + /** Using Dialog without open prop is forbidden, so we acknowledge that but since we need to test it we silence the console */ |
| 388 | + jest.spyOn(console, 'error').mockImplementation(() => { |
| 389 | + // do nothing |
| 390 | + }) |
| 391 | + |
| 392 | + const caller = jest.fn<void, unknown[]>() |
| 393 | + const service = { |
| 394 | + call: (...args: unknown[]): void => caller(...args) |
| 395 | + } as WebviewService |
| 396 | + |
| 397 | + const { unmount } = render( |
| 398 | + <WebviewIntentProvider webviewService={service}> |
| 399 | + <BreakpointsProvider> |
| 400 | + <Dialog /> |
| 401 | + </BreakpointsProvider> |
| 402 | + </WebviewIntentProvider> |
| 403 | + ) |
| 404 | + |
| 405 | + expect(caller).not.toHaveBeenCalled() |
| 406 | + |
| 407 | + unmount() |
| 408 | + |
| 409 | + /** As it was mounted without the open prop, and we never changed it, then it never sent an onMount message. |
| 410 | + * In this scenario, it is only logical that it does not send an unMount message either since it never showed up. |
| 411 | + */ |
| 412 | + expect(caller).not.toHaveBeenCalled() |
| 413 | +}) |
| 414 | + |
| 415 | +it('when provided with a faulty <Dialog /> that has no open prop, and then fixed at runtime, it should emit mount and unmount messages as shown earlier', () => { |
| 416 | + const caller = jest.fn<void, unknown[]>() |
| 417 | + const service = { |
| 418 | + call: (...args: unknown[]): void => caller(...args) |
| 419 | + } as WebviewService |
| 420 | + |
| 421 | + const { rerender, unmount } = render( |
| 422 | + <WebviewIntentProvider webviewService={service}> |
| 423 | + <BreakpointsProvider> |
| 424 | + <Dialog /> |
| 425 | + </BreakpointsProvider> |
| 426 | + </WebviewIntentProvider> |
| 427 | + ) |
| 428 | + |
| 429 | + expect(caller).not.toHaveBeenCalled() |
| 430 | + |
| 431 | + rerender( |
| 432 | + <WebviewIntentProvider webviewService={service}> |
| 433 | + <BreakpointsProvider> |
| 434 | + <Dialog open /> |
| 435 | + </BreakpointsProvider> |
| 436 | + </WebviewIntentProvider> |
| 437 | + ) |
| 438 | + |
| 439 | + expect(caller).toHaveBeenNthCalledWith( |
| 440 | + 1, |
| 441 | + 'setFlagshipUI', |
| 442 | + onOpenMountExpected, |
| 443 | + 'cozy-ui/Dialog (onOpenMount)' |
| 444 | + ) |
| 445 | + |
| 446 | + rerender( |
| 447 | + <WebviewIntentProvider webviewService={service}> |
| 448 | + <BreakpointsProvider> |
| 449 | + <Dialog open={false} /> |
| 450 | + </BreakpointsProvider> |
| 451 | + </WebviewIntentProvider> |
| 452 | + ) |
| 453 | + |
| 454 | + expect(caller).toHaveBeenNthCalledWith( |
| 455 | + 2, |
| 456 | + 'setFlagshipUI', |
| 457 | + onOpenUnmountExpected, |
| 458 | + 'cozy-ui/Dialog (onOpenUnmount)' |
| 459 | + ) |
| 460 | + |
| 461 | + unmount() |
| 462 | + |
| 463 | + /** Checking it doesn't send two unmount messages in a row */ |
| 464 | + expect(caller).toHaveBeenCalledTimes(2) |
| 465 | +}) |
0 commit comments