-
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
5 changed files
with
109 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 { useAlert } 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 { ManualInstall } from './ManualInstall.js' | ||
|
||
jest.mock('@dhis2/app-runtime', () => ({ | ||
...jest.requireActual('@dhis2/app-runtime'), | ||
useAlert: jest.fn(() => jest.fn()), | ||
})) | ||
|
||
describe('Manual Install', () => { | ||
const mockAlert = jest.fn() | ||
let fetchSpy | ||
|
||
beforeEach(() => { | ||
global.fetch = jest.fn() | ||
useAlert.mockImplementation(() => ({ show: mockAlert })) | ||
}) | ||
|
||
afterEach(() => { | ||
delete global.fetch | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should call the correct upload endpoint ', async () => { | ||
fetchSpy = jest.spyOn(global, 'fetch').mockResolvedValueOnce({ | ||
json: () => Promise.resolve({ app_hub_id: 'some_apphub_id' }), | ||
}) | ||
|
||
const { getByTestId, findByText } = render(<ManualInstall />) | ||
|
||
const fileInput = getByTestId('file-upload') | ||
userEvent.upload(fileInput, 'testfile') | ||
|
||
await findByText(/Uploading/i) | ||
|
||
expect(fetchSpy).toHaveBeenCalledWith( | ||
'../api/apps', | ||
expect.objectContaining({ | ||
method: 'post', | ||
}) | ||
) | ||
}) | ||
|
||
it('should show the alert', async () => { | ||
fetchSpy = jest.spyOn(global, 'fetch').mockResolvedValueOnce({ | ||
json: () => Promise.resolve({ app_hub_id: 'some_apphub_id' }), | ||
}) | ||
|
||
const { getByTestId, findByText } = render(<ManualInstall />) | ||
|
||
const fileInput = getByTestId('file-upload') | ||
userEvent.upload(fileInput, 'testfile') | ||
|
||
await findByText(/Uploading/i) | ||
|
||
expect(mockAlert).toHaveBeenCalledWith({ id: 'some_apphub_id' }) | ||
}) | ||
|
||
it('should work with an empty response (pre v41)', async () => { | ||
fetchSpy = jest.spyOn(global, 'fetch').mockResolvedValueOnce({ | ||
json: () => Promise.resolve(), | ||
}) | ||
|
||
const { getByTestId, findByText } = render(<ManualInstall />) | ||
|
||
const fileInput = getByTestId('file-upload') | ||
userEvent.upload(fileInput, 'testfile') | ||
|
||
await findByText(/Uploading/i) | ||
|
||
expect(mockAlert).toHaveBeenCalledWith({ id: undefined }) | ||
}) | ||
|
||
it('should show an error if it fails', async () => { | ||
fetchSpy = jest.spyOn(global, 'fetch').mockResolvedValueOnce({ | ||
json: () => { | ||
throw 'upload failed' | ||
}, | ||
}) | ||
|
||
const { getByTestId, findByText } = render(<ManualInstall />) | ||
|
||
const fileInput = getByTestId('file-upload') | ||
userEvent.upload(fileInput, 'testfile') | ||
|
||
await findByText(/Uploading/i) | ||
|
||
expect(mockAlert).toHaveBeenCalledWith({ error: 'upload failed' }) | ||
}) | ||
}) |
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