AppsPage.test.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React from 'react';
  2. import { fireEvent, render, screen, waitFor } from '../../../../../tests/test-utils';
  3. import appHandlers, { mockInstalledAppIds } from '../../../../mocks/handlers/appHandlers';
  4. import { server } from '../../../../mocks/server';
  5. import { AppsPage } from './AppsPage';
  6. const pushFn = jest.fn();
  7. jest.mock('next/router', () => {
  8. const actualRouter = jest.requireActual('next-router-mock');
  9. return {
  10. ...actualRouter,
  11. useRouter: () => ({
  12. ...actualRouter.useRouter(),
  13. push: pushFn,
  14. }),
  15. };
  16. });
  17. describe('AppsPage', () => {
  18. it('should render', async () => {
  19. // Arrange
  20. render(<AppsPage />);
  21. // Assert
  22. await waitFor(() => {
  23. expect(screen.getByTestId('apps-list')).toBeInTheDocument();
  24. });
  25. });
  26. it('should render all installed apps', async () => {
  27. // Arrange
  28. render(<AppsPage />);
  29. await waitFor(() => {
  30. expect(screen.getByTestId('apps-list')).toBeInTheDocument();
  31. });
  32. // Assert
  33. const displayedAppIds = screen.getAllByTestId(/app-tile-/);
  34. expect(displayedAppIds).toHaveLength(mockInstalledAppIds.length);
  35. });
  36. it('Should not render app tile if app info is not available', async () => {
  37. // Arrange
  38. server.use(appHandlers.installedAppsNoInfo);
  39. render(<AppsPage />);
  40. await waitFor(() => {
  41. expect(screen.getByTestId('apps-list')).toBeInTheDocument();
  42. });
  43. // Assert
  44. expect(screen.queryByTestId(/app-tile-/)).not.toBeInTheDocument();
  45. });
  46. });
  47. describe('AppsPage - Empty', () => {
  48. beforeEach(() => {
  49. server.use(appHandlers.installedAppsEmpty);
  50. });
  51. it('should render empty page if no app is installed', async () => {
  52. // Arrange
  53. render(<AppsPage />);
  54. await waitFor(() => {
  55. expect(screen.getByTestId('empty-page')).toBeInTheDocument();
  56. });
  57. // Assert
  58. expect(screen.queryByTestId('apps-list')).not.toBeInTheDocument();
  59. });
  60. it('should trigger navigation to app store on click on action button', async () => {
  61. // Arrange
  62. render(<AppsPage />);
  63. await waitFor(() => {
  64. expect(screen.getByTestId('empty-page')).toBeInTheDocument();
  65. });
  66. // Act
  67. const actionButton = screen.getByTestId('empty-page-action');
  68. await fireEvent.click(actionButton);
  69. // Assert
  70. expect(actionButton).toHaveTextContent('Go to app store');
  71. expect(pushFn).toHaveBeenCalledWith('/app-store');
  72. });
  73. });
  74. describe('AppsPage - Error', () => {
  75. beforeEach(() => {
  76. server.use(appHandlers.installedAppsError);
  77. });
  78. it('should render error page if an error occurs', async () => {
  79. render(<AppsPage />);
  80. await waitFor(() => {
  81. expect(screen.getByTestId('error-page')).toBeInTheDocument();
  82. });
  83. expect(screen.getByText('test-error')).toHaveTextContent('test-error');
  84. expect(screen.queryByTestId('apps-list')).not.toBeInTheDocument();
  85. });
  86. });