mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-06-28 23:49:26 +02:00
Renaming state operations from 'campaign' to 'record'. Implemented API endpoint retrieval of GridField data. Added more mock data into CampaignAdmin (rather than hardcoding in client), to be replaced by CampaignAdmin API endpoint querying the real datamodel. Using more native isomorphic-fetch instead of jQuery.ajax to minimise dependencies and get into a more forward-thinking API. Also catching errors in ReactJS API backend: Emulate jQuery.ajax() behaviour. Might change at a later point if we have a general purpose backend with a promise-based catch() implementation.
103 lines
2.7 KiB
JavaScript
103 lines
2.7 KiB
JavaScript
jest.mock('isomorphic-fetch');
|
|
jest.unmock('../silverstripe-backend');
|
|
|
|
import fetch from 'isomorphic-fetch';
|
|
import backend from '../silverstripe-backend';
|
|
|
|
var getFetchMock = function(data) {
|
|
let mock = jest.genMockFunction();
|
|
let promise = new Promise((resolve, reject) => {
|
|
process.nextTick(() => resolve(data));
|
|
});
|
|
mock.mockReturnValue(promise);
|
|
|
|
return mock;
|
|
};
|
|
|
|
describe('SilverStripeBackend', () => {
|
|
|
|
beforeAll(() => {
|
|
let fetchMock = getFetchMock();
|
|
backend.fetch = fetchMock;
|
|
});
|
|
|
|
describe('get()', () => {
|
|
|
|
it('should return a promise', () => {
|
|
var promise = backend.get('http://example.com');
|
|
expect(typeof promise).toBe('object');
|
|
});
|
|
|
|
it('should send a GET request to an endpoint', () => {
|
|
backend.get('http://example.com');
|
|
expect(backend.fetch).toBeCalledWith(
|
|
'http://example.com',
|
|
{method: 'get', credentials: 'same-origin'}
|
|
);
|
|
});
|
|
|
|
});
|
|
|
|
describe('post()', () => {
|
|
|
|
it('should return a promise', () => {
|
|
var promise = backend.get('http://example.com/item');
|
|
expect(typeof promise).toBe('object');
|
|
});
|
|
|
|
it('should send a POST request to an endpoint', () => {
|
|
const postData = { id: 1 };
|
|
|
|
backend.post('http://example.com', postData);
|
|
|
|
expect(backend.fetch).toBeCalledWith(
|
|
'http://example.com',
|
|
{method: 'post', body: postData, credentials: 'same-origin'}
|
|
);
|
|
});
|
|
|
|
});
|
|
|
|
describe('put()', () => {
|
|
|
|
it('should return a promise', () => {
|
|
var promise = backend.get('http://example.com/item');
|
|
expect(typeof promise).toBe('object');
|
|
});
|
|
|
|
it('should send a PUT request to an endpoint', () => {
|
|
const putData = { id: 1 };
|
|
|
|
backend.put('http://example.com', putData);
|
|
|
|
expect(backend.fetch).toBeCalledWith(
|
|
'http://example.com',
|
|
{method: 'put', body: putData, credentials: 'same-origin'}
|
|
);
|
|
});
|
|
|
|
});
|
|
|
|
describe('delete()', () => {
|
|
|
|
it('should return a promise', () => {
|
|
var promise = backend.get('http://example.com/item');
|
|
|
|
expect(typeof promise).toBe('object');
|
|
});
|
|
|
|
it('should send a DELETE request to an endpoint', () => {
|
|
const deleteData = { id: 1 };
|
|
|
|
backend.delete('http://example.com', deleteData);
|
|
|
|
expect(backend.fetch).toBeCalledWith(
|
|
'http://example.com',
|
|
{method: 'delete', body: deleteData, credentials: 'same-origin'}
|
|
);
|
|
});
|
|
|
|
});
|
|
|
|
});
|