-
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.
fix(DHIS2-13294): add link to navigate to app after install (#555)
* fix(DHIS2-13294): add link to navigate to app after install * test: add tests for ManualInstall * fix: ensure logic does not fail with empty response pre v40 * refactor: update label for redirect button
- Loading branch information
Showing
7 changed files
with
691 additions
and
628 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,92 @@ | ||
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({ | ||
text: () => | ||
Promise.resolve( | ||
JSON.stringify({ 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('View app details')) | ||
expect(historyPush).toHaveBeenCalledWith('/app/some_apphub_id') | ||
}) | ||
|
||
it('should work with an empty response (pre v41)', async () => { | ||
jest.spyOn(global, 'fetch').mockResolvedValueOnce({ | ||
text: () => null, | ||
}) | ||
|
||
const { getByTestId, findByText, queryByText } = renderWithProvider( | ||
<ManualInstall /> | ||
) | ||
|
||
const fileInput = getByTestId('file-upload') | ||
userEvent.upload(fileInput, 'testfile') | ||
|
||
await findByText('App installed successfully') | ||
expect(queryByText('View app details')).not.toBeInTheDocument() | ||
expect(historyPush).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should show an error if it fails', async () => { | ||
jest.spyOn(global, 'fetch').mockRejectedValue('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('View app details')).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' |
Oops, something went wrong.
27e157a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://dhis2-app-management.netlify.app as production
🚀 Deployed on https://66e138901745c59f298680e3--dhis2-app-management.netlify.app