This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #816 from mturley/simple-api-mock
Replace broken axios API mock adapter, fix broken tests (cherry picked from commit ed5249d) https://bugzilla.redhat.com/show_bug.cgi?id=1657285
- Loading branch information
1 parent
fd367df
commit 8e04c01
Showing
9 changed files
with
111 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { configure } from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import { APImock } from './app/javascript/common/mockRequests'; | ||
|
||
configure({ adapter: new Adapter() }); | ||
|
||
// Mocking translation function | ||
global.__ = str => str; | ||
global.n__ = str => str; | ||
global.sprintf = str => str; | ||
global.Jed = { sprintf: str => str }; | ||
global.API.get = jest.fn(() => Promise.resolve()); | ||
global.API.post = jest.fn(() => Promise.resolve()); | ||
|
||
APImock.reset(); | ||
|
||
global.API.get = jest.fn(url => APImock.respond('GET', url)); | ||
global.API.put = jest.fn(url => APImock.respond('PUT', url)); | ||
global.API.post = jest.fn(url => APImock.respond('POST', url)); | ||
global.API.delete = jest.fn(url => APImock.respond('DELETE', url)); |
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 |
---|---|---|
@@ -1,15 +1,27 @@ | ||
import axios from 'axios'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
let mockedRequests; | ||
|
||
const mock = new MockAdapter(axios); | ||
const methods = { | ||
GET: 'onGet', | ||
POST: 'onPost', | ||
PUT: 'onPut', | ||
DELETE: 'onDelete' | ||
export const APImock = { | ||
reset: () => { | ||
mockedRequests = { GET: {}, POST: {}, PUT: {}, DELETE: {} }; | ||
}, | ||
mock: (method, url, status, response) => { | ||
mockedRequests[method][url] = { status, response }; | ||
}, | ||
respond: (method, url) => { | ||
const mockMatchingUrl = mockedRequests[method][url]; | ||
const mocksMatchingMethod = Object.values(mockedRequests[method]); | ||
const mocked = mockMatchingUrl || (mocksMatchingMethod.length > 0 && mocksMatchingMethod[0]); | ||
if (mocked) { | ||
if (mocked.status === 200) { | ||
return Promise.resolve(mocked.response); | ||
} | ||
return Promise.reject(new Error('<mocked error>')); | ||
} | ||
return Promise.reject(new Error(`<no such mocked request ${method} ${url}>`)); | ||
} | ||
}; | ||
|
||
export const mockRequest = ({ method = 'GET', url, data = null, status = 200, response = null }) => | ||
mock[methods[method]](url, data).reply(status, response); | ||
export const mockRequest = ({ method = 'GET', url, status = 200, response = null }) => | ||
APImock.mock(method, url, status, response); | ||
|
||
export const mockReset = () => mock.reset(); | ||
export const mockReset = () => APImock.reset(); |
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
27 changes: 25 additions & 2 deletions
27
...javascript/react/screens/App/Overview/__test__/__snapshots__/OverviewActions.test.js.snap
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