Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('Test fetch function on 404 response', () => {
const name = 'test-rest-api';
const endpoint = 'books';
const items = [];
const params = {context: 'view'};
const statusText = 'not-found';
const mockData = {
status: 404,
statusText: statusText,
data: items,
total: items.length,
totalPages: 1
};
require('isomorphic-fetch').__setMockData(mockData);
let called = 0;
const dispatch = action => {
if (called++) {
expect(require('isomorphic-fetch').__getRequestedUrl(mockData)).toBe(`http://wordpress.test/wp-json/wp/v2/${endpoint}?context=view`);
expect(action.type).toBe(`@@wp/${name}/fetched/${endpoint}`);
expect(action.total).toBeUndefined();
expect(action.totalPages).toBeUndefined();
expect(action.results).toBeUndefined();
expect(action.ok).toBeFalsy();
expect(action.message).toBe(statusText);
} else {
expect(action.type).toBe(`@@wp/${name}/fetching/${endpoint}`);
}
test('Test fetchEndpointById function on reject response', () => {
const name = 'test-rest-api';
const endpoint = 'books';
const endpoint2 = 'chapters';
const params = {context: 'view'};
const statusText = '404 not found';
const id = faker.random.number();
const mockData = {
reject: true,
statusText: statusText
};
require('isomorphic-fetch').__setMockData(mockData);
let called = 0;
const dispatch = action => {
if (called++) {
expect(require('isomorphic-fetch').__getRequestedUrl(mockData)).toBe(`http://wordpress.test/wp-json/wp/v2/${endpoint}/${id}/${endpoint2}?context=view`);
expect(action.type).toBe(`@@wp/${name}/fetched-by-id/${endpoint}/${endpoint2}`);
expect(action.result).toBeUndefined();
expect(action.ok).toBeFalsy();
expect(action.message).toBe(statusText);
} else {
expect(action.type).toBe(`@@wp/${name}/fetching-by-id/${endpoint}/${endpoint2}`);
}
expect(action.params).toEqual(params);
expect(action.id).toBe(id);
test('Test fetch function on reject response', () => {
const name = 'test-rest-api';
const endpoint = 'books';
const endpoint2 = 'chapters';
const params = {context: 'view'};
const statusText = '404 not found';
const mockData = {
reject: true,
statusText: statusText
};
require('isomorphic-fetch').__setMockData(mockData);
let called = 0;
const dispatch = action => {
if (called++) {
expect(require('isomorphic-fetch').__getRequestedUrl(mockData)).toBe(`http://wordpress.test/wp-json/wp/v2/${endpoint}/${endpoint2}?context=view`);
expect(action.type).toBe(`@@wp/${name}/fetched/${endpoint}/${endpoint2}`);
expect(action.total).toBeUndefined();
expect(action.totalPages).toBeUndefined();
expect(action.results).toBeUndefined();
expect(action.ok).toBeFalsy();
expect(action.message).toBe(statusText);
} else {
expect(action.type).toBe(`@@wp/${name}/fetching/${endpoint}/${endpoint2}`);
}
test('Test fetchById function on 404 response', () => {
const name = 'test-rest-api';
const endpoint = 'books';
const item = {};
const params = {context: 'view'};
const statusText = 'not-found';
const id = faker.random.number();
const mockData = {
status: 404,
statusText: statusText,
data: item
};
require('isomorphic-fetch').__setMockData(mockData);
let called = 0;
const dispatch = action => {
if (called++) {
expect(require('isomorphic-fetch').__getRequestedUrl(mockData)).toBe(`http://wordpress.test/wp-json/wp/v2/${endpoint}/${id}?context=view`);
expect(action.type).toBe(`@@wp/${name}/fetched-by-id/${endpoint}`);
expect(action.result).toBeUndefined();
expect(action.ok).toBeFalsy();
expect(action.message).toBe(statusText);
} else {
expect(action.type).toBe(`@@wp/${name}/fetching-by-id/${endpoint}`);
}
expect(action.params).toEqual(params);
expect(action.id).toBe(id);