-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Provider } from '@dhis2/app-runtime' | ||
import { render } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import React from 'react' | ||
import '@testing-library/jest-dom' | ||
import { useHistory } from 'react-router-dom' | ||
import { MockAlertStack } from '../../test-utils/index.js' | ||
import { ManualInstall } from './ManualInstall.js' | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
useHistory: jest.fn(() => ({ | ||
push: jest.fn(), | ||
})), | ||
})) | ||
|
||
const renderWithProvider = (component) => { | ||
return render(component, { | ||
wrapper: ({ children }) => { | ||
return ( | ||
<Provider config={{}}> | ||
{children} | ||
<MockAlertStack /> | ||
</Provider> | ||
) | ||
}, | ||
}) | ||
} | ||
describe('Manual Install', () => { | ||
const historyPush = jest.fn() | ||
|
||
beforeEach(() => { | ||
global.fetch = jest.fn() | ||
useHistory.mockImplementation(() => ({ push: historyPush })) | ||
}) | ||
|
||
afterEach(() => { | ||
delete global.fetch | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should allow navigating to the app', async () => { | ||
jest.spyOn(global, 'fetch').mockResolvedValueOnce({ | ||
json: () => Promise.resolve({ app_hub_id: 'some_apphub_id' }), | ||
}) | ||
|
||
const { getByTestId, getByText, findByText } = renderWithProvider( | ||
<ManualInstall /> | ||
) | ||
|
||
const fileInput = getByTestId('file-upload') | ||
userEvent.upload(fileInput, 'testfile') | ||
|
||
await findByText('App installed successfully') | ||
await userEvent.click(getByText('Go to app')) | ||
expect(historyPush).toHaveBeenCalledWith('/app/some_apphub_id') | ||
}) | ||
|
||
it('should work with an empty response (pre v41)', async () => { | ||
jest.spyOn(global, 'fetch').mockResolvedValueOnce({ | ||
json: () => Promise.resolve(), | ||
}) | ||
|
||
const { getByTestId, findByText, queryByText } = renderWithProvider( | ||
<ManualInstall /> | ||
) | ||
|
||
const fileInput = getByTestId('file-upload') | ||
userEvent.upload(fileInput, 'testfile') | ||
|
||
await findByText('App installed successfully') | ||
expect(queryByText('Go to app')).not.toBeInTheDocument() | ||
expect(historyPush).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should show an error if it fails', async () => { | ||
jest.spyOn(global, 'fetch').mockResolvedValueOnce({ | ||
json: () => { | ||
throw 'upload failed' | ||
}, | ||
}) | ||
|
||
const { getByTestId, findByText, queryByText } = renderWithProvider( | ||
<ManualInstall /> | ||
) | ||
|
||
const fileInput = getByTestId('file-upload') | ||
userEvent.upload(fileInput, 'testfile') | ||
|
||
await findByText('Failed to install app:') | ||
expect(queryByText('Go to app')).not.toBeInTheDocument() | ||
expect(historyPush).not.toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useAlerts } from '@dhis2/app-runtime' | ||
import PropTypes from 'prop-types' | ||
import React, { useEffect } from 'react' | ||
|
||
const MockAlert = ({ alert }) => { | ||
useEffect(() => { | ||
if (alert.options?.duration) { | ||
setTimeout(() => alert.remove(), alert.options?.duration) | ||
} | ||
}, [alert]) | ||
return ( | ||
<div style={{ backgroundColor: '#CCC', padding: 8 }}> | ||
{alert.message} | ||
{alert?.options?.actions?.map((action, i) => ( | ||
<button key={i} onClick={action.onClick}> | ||
{action.label} | ||
</button> | ||
))} | ||
</div> | ||
) | ||
} | ||
|
||
MockAlert.propTypes = { | ||
alert: PropTypes.shape({ | ||
message: PropTypes.string, | ||
options: PropTypes.shape({ | ||
actions: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
label: PropTypes.string, | ||
onClick: PropTypes.func, | ||
}) | ||
), | ||
duration: PropTypes.number, | ||
}), | ||
remove: PropTypes.func, | ||
}), | ||
} | ||
|
||
export const MockAlertStack = () => { | ||
const alerts = useAlerts() | ||
|
||
return ( | ||
<div style={{ position: 'absolute', bottom: 16, right: 16 }}> | ||
{alerts.map((alert) => ( | ||
<MockAlert key={alert.id} alert={alert} /> | ||
))} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './MockAlertStack.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters