|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, render } from 'design/utils/testing'; |
| 3 | +import { ShareFeedback } from './ShareFeedback'; |
| 4 | +import { MockAppContextProvider } from 'teleterm/ui/fixtures/MockAppContextProvider'; |
| 5 | +import { Cluster } from 'teleterm/services/tshd/v1/cluster_pb'; |
| 6 | +import { MockAppContext } from 'teleterm/ui/fixtures/mocks'; |
| 7 | + |
| 8 | +test('email field is not prefilled with the username if is not an email', () => { |
| 9 | + const appContext = new MockAppContext(); |
| 10 | + const clusterUri = '/clusters/localhost'; |
| 11 | + jest |
| 12 | + .spyOn(appContext.clustersService, 'findCluster') |
| 13 | + .mockImplementation(() => { |
| 14 | + return { |
| 15 | + loggedInUser: { name: 'alice' }, |
| 16 | + } as Cluster.AsObject; |
| 17 | + }); |
| 18 | + |
| 19 | + jest |
| 20 | + .spyOn(appContext.workspacesService, 'getRootClusterUri') |
| 21 | + .mockReturnValue(clusterUri); |
| 22 | + |
| 23 | + const { getByLabelText } = render( |
| 24 | + <MockAppContextProvider appContext={appContext}> |
| 25 | + <ShareFeedback onClose={undefined} /> |
| 26 | + </MockAppContextProvider> |
| 27 | + ); |
| 28 | + |
| 29 | + expect(appContext.clustersService.findCluster).toHaveBeenCalledWith( |
| 30 | + clusterUri |
| 31 | + ); |
| 32 | + expect(getByLabelText('Email Address')).toHaveValue(''); |
| 33 | +}); |
| 34 | + |
| 35 | +test('email field is prefilled with the username if it looks like an email', () => { |
| 36 | + const appContext = new MockAppContext(); |
| 37 | + const clusterUri = '/clusters/production'; |
| 38 | + jest |
| 39 | + .spyOn(appContext.clustersService, 'findCluster') |
| 40 | + .mockImplementation(() => { |
| 41 | + return { |
| 42 | + loggedInUser: { |
| 43 | + name: 'bob@prod.com', |
| 44 | + }, |
| 45 | + } as Cluster.AsObject; |
| 46 | + }); |
| 47 | + |
| 48 | + jest |
| 49 | + .spyOn(appContext.workspacesService, 'getRootClusterUri') |
| 50 | + .mockReturnValue(clusterUri); |
| 51 | + |
| 52 | + const { getByLabelText } = render( |
| 53 | + <MockAppContextProvider appContext={appContext}> |
| 54 | + <ShareFeedback onClose={undefined} /> |
| 55 | + </MockAppContextProvider> |
| 56 | + ); |
| 57 | + |
| 58 | + expect(appContext.clustersService.findCluster).toHaveBeenCalledWith( |
| 59 | + clusterUri |
| 60 | + ); |
| 61 | + expect(getByLabelText('Email Address')).toHaveValue('bob@prod.com'); |
| 62 | +}); |
| 63 | + |
| 64 | +test('onClose is called when close button is clicked', () => { |
| 65 | + const appContext = new MockAppContext(); |
| 66 | + const handleClose = jest.fn(); |
| 67 | + |
| 68 | + const { getByTitle } = render( |
| 69 | + <MockAppContextProvider appContext={appContext}> |
| 70 | + <ShareFeedback onClose={handleClose} /> |
| 71 | + </MockAppContextProvider> |
| 72 | + ); |
| 73 | + |
| 74 | + fireEvent.click(getByTitle('Close')); |
| 75 | + |
| 76 | + expect(handleClose).toHaveBeenCalledTimes(1); |
| 77 | +}); |
0 commit comments