-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
1bca36c
commit 3338409
Showing
57 changed files
with
2,086 additions
and
558 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { apiClient, apiHeaders } from './Api' | ||
|
||
describe('apiClient', () => { | ||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
it('should return data on success', async () => { | ||
const data = { status: 'success', data: { features: { telegramEnabled: true } } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await apiClient('/admin/features', { headers: apiHeaders('token') }) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith('/admin/features', { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should throw error on non-200 status', async () => { | ||
const data = { status: 'error', error: { code: 500, message: 'Internal Server Error' } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await apiClient('/admin/features', { headers: apiHeaders('token') }) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith('/admin/features', { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should throw error on network error', async () => { | ||
fetchMock.mockReject(new Error('Network error')) | ||
const response = await apiClient('/admin/features', { headers: apiHeaders('token') }) | ||
expect(response).toEqual({ status: 'error', error: { code: 500, message: 'Network error' } }) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith('/admin/features', { headers: apiHeaders('token') }) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import { login } from './Auth' | ||
|
||
describe('Auth', function () { | ||
describe('Auth', () => { | ||
beforeEach(() => { | ||
fetch.resetMocks() | ||
fetch.doMock() | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
test('login failed', function () { | ||
fetch.mockResponse( | ||
it('should fail to login when credentials wrong', () => { | ||
fetchMock.mockResponse( | ||
JSON.stringify({ | ||
data: {}, | ||
status: 'error', | ||
|
@@ -19,19 +18,19 @@ describe('Auth', function () { | |
expect(login('[email protected]', 'password')).rejects.toThrow('Login failed') | ||
}) | ||
|
||
test('server error', function () { | ||
fetch.mockReject() | ||
it('should fail when network fails', () => { | ||
fetchMock.mockReject() | ||
|
||
expect(login('[email protected]', 'password')).rejects.toThrow('Login failed') | ||
}) | ||
|
||
test('login successful', function () { | ||
it('should succeed', () => { | ||
const response = { | ||
code: 200, | ||
expire: '2022-10-01T18:22:37+02:00', | ||
token: 'token', | ||
} | ||
fetch.mockResponse(JSON.stringify(response), { status: 200 }) | ||
fetchMock.mockResponse(JSON.stringify(response), { status: 200 }) | ||
|
||
expect(login('[email protected]', 'password')).resolves.toEqual(response) | ||
}) | ||
|
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,34 @@ | ||
import { ApiUrl, apiHeaders } from './Api' | ||
import { fetchFeaturesApi } from './Features' | ||
|
||
describe('fetchFeaturesApi', () => { | ||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
it('should return data on success', async () => { | ||
const data = { features: { telegramEnabled: true } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await fetchFeaturesApi('token') | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/features`, { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should return data on error', async () => { | ||
const data = { status: 'error', error: { code: 500, message: 'Internal Server Error' } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await fetchFeaturesApi('token') | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/features`, { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should return data on network error', async () => { | ||
fetchMock.mockReject(new Error('Network error')) | ||
const response = await fetchFeaturesApi('token') | ||
expect(response).toEqual({ status: 'error', error: { code: 500, message: 'Network error' } }) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/features`, { headers: apiHeaders('token') }) | ||
}) | ||
}) |
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,167 @@ | ||
import { ApiUrl, apiHeaders } from './Api' | ||
import { Message, deleteMessageApi, fetchMessagesApi, postMessageApi } from './Message' | ||
|
||
describe('fetchMessagesApi', () => { | ||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
it('should return data on success', async () => { | ||
const data = { status: 'success', data: { messages: [] } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await fetchMessagesApi('token', 1) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages?limit=10`, { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should return data with before parameter', async () => { | ||
const data = { status: 'success', data: { messages: [] } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await fetchMessagesApi('token', 1, 2) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages?limit=10&before=2`, { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should return data with after parameter', async () => { | ||
const data = { status: 'success', data: { messages: [] } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await fetchMessagesApi('token', 1, undefined, 3) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages?limit=10&after=3`, { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should return data with before and after parameters', async () => { | ||
const data = { status: 'success', data: { messages: [] } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await fetchMessagesApi('token', 1, 2, 3) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages?limit=10&before=2&after=3`, { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should throw error on non-200 status', async () => { | ||
const data = { status: 'error', error: { code: 500, message: 'Internal Server Error' } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await fetchMessagesApi('token', 1) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages?limit=10`, { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should throw error on network error', async () => { | ||
fetchMock.mockReject(new Error('Network error')) | ||
const response = await fetchMessagesApi('token', 1) | ||
expect(response).toEqual({ status: 'error', error: { code: 500, message: 'Network error' } }) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages?limit=10`, { headers: apiHeaders('token') }) | ||
}) | ||
|
||
it('should return data with custom limit', async () => { | ||
const data = { status: 'success', data: { messages: [] } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await fetchMessagesApi('token', 1, undefined, undefined, 5) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages?limit=5`, { headers: apiHeaders('token') }) | ||
}) | ||
}) | ||
|
||
describe('postMessageApi', () => { | ||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
it('should return data on success', async () => { | ||
const data = { status: 'success', data: { message: {} } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await postMessageApi('token', 'ticker', 'text', { type: 'FeatureCollection', features: [] }, []) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/ticker/messages`, { | ||
headers: apiHeaders('token'), | ||
method: 'post', | ||
body: JSON.stringify({ | ||
text: 'text', | ||
geoInformation: { type: 'FeatureCollection', features: [] }, | ||
attachments: [], | ||
}), | ||
}) | ||
}) | ||
|
||
it('should throw error on non-200 status', async () => { | ||
const data = { status: 'error', error: { code: 500, message: 'Internal Server Error' } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await postMessageApi('token', 'ticker', 'text', { type: 'FeatureCollection', features: [] }, []) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/ticker/messages`, { | ||
headers: apiHeaders('token'), | ||
method: 'post', | ||
body: JSON.stringify({ | ||
text: 'text', | ||
geoInformation: { type: 'FeatureCollection', features: [] }, | ||
attachments: [], | ||
}), | ||
}) | ||
}) | ||
|
||
it('should throw error on network error', async () => { | ||
fetchMock.mockReject(new Error('Network error')) | ||
const response = await postMessageApi('token', 'ticker', 'text', { type: 'FeatureCollection', features: [] }, []) | ||
expect(response).toEqual({ status: 'error', error: { code: 500, message: 'Network error' } }) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/ticker/messages`, { | ||
headers: apiHeaders('token'), | ||
method: 'post', | ||
body: JSON.stringify({ | ||
text: 'text', | ||
geoInformation: { type: 'FeatureCollection', features: [] }, | ||
attachments: [], | ||
}), | ||
}) | ||
}) | ||
}) | ||
|
||
describe('deleteMessageApi', () => { | ||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
it('should return data on success', async () => { | ||
const data = { status: 'success' } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await deleteMessageApi('token', { id: 1, ticker: 1 } as Message) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages/1`, { | ||
headers: apiHeaders('token'), | ||
method: 'delete', | ||
}) | ||
}) | ||
|
||
it('should throw error on non-200 status', async () => { | ||
const data = { status: 'error', error: { code: 500, message: 'Internal Server Error' } } | ||
fetchMock.mockResponseOnce(JSON.stringify(data)) | ||
const response = await deleteMessageApi('token', { id: 1, ticker: 1 } as Message) | ||
expect(response).toEqual(data) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages/1`, { | ||
headers: apiHeaders('token'), | ||
method: 'delete', | ||
}) | ||
}) | ||
|
||
it('should throw error on network error', async () => { | ||
fetchMock.mockReject(new Error('Network error')) | ||
const response = await deleteMessageApi('token', { id: 1, ticker: 1 } as Message) | ||
expect(response).toEqual({ status: 'error', error: { code: 500, message: 'Network error' } }) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/tickers/1/messages/1`, { | ||
headers: apiHeaders('token'), | ||
method: 'delete', | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.