@kineticdata/react
Version:
A React library for the Kinetic Platform
230 lines (229 loc) • 8.22 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2"));
var _axios = _interopRequireDefault(require("axios"));
var _promises = require("../../../tests/utils/promises");
var _bridgedresources = require("./bridgedresources");
// Mock out the bundle object from a dependency.
jest.mock('../../helpers', function () {
return {
bundle: {
apiLocation: function apiLocation() {
return 'user/app/api/v1';
},
spaceLocation: function spaceLocation() {
return 'user';
},
kappSlug: function kappSlug() {
return 'mock-kapp';
}
}
};
});
describe('bridged resource api', function () {
var options;
beforeEach(function () {
options = {
kappSlug: 'kappslug',
formSlug: 'formslug',
bridgedResourceName: 'Collection'
};
});
describe('#bridgedResourceUrl', function () {
describe('options', function () {
test('with valid parameters does not throw an exception', function () {
expect(function () {
(0, _bridgedresources.bridgedResourceUrl)(options);
}).not.toThrow();
});
test('missing "formSlug" throws an exception', function () {
delete options.formSlug;
expect(function () {
(0, _bridgedresources.bridgedResourceUrl)(options);
}).toThrow();
});
test('missing "bridgedResourceName" throws an exception', function () {
expect(function () {
(0, _bridgedresources.bridgedResourceUrl)({
kappSlug: 'kapp',
formSlug: 'form'
});
}).toThrow();
});
});
test('normal output', function () {
expect((0, _bridgedresources.bridgedResourceUrl)(options)).toBe('user/kappslug/formslug/bridgedResources/Collection');
});
test('datastore form', function () {
expect((0, _bridgedresources.bridgedResourceUrl)((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, options), {}, {
datastore: true
}))).toBe('user/datastore/formslug/bridgedResources/Collection');
});
test('default the kapp option', function () {
delete options.kappSlug;
expect((0, _bridgedresources.bridgedResourceUrl)(options)).toBe('user/mock-kapp/formslug/bridgedResources/Collection');
});
});
describe('#bridgedResourceData', function () {
test('without data options', function () {
expect((0, _bridgedresources.bridgedResourceData)(options)).toEqual('');
});
test('with limit option', function () {
options.limit = 10;
expect((0, _bridgedresources.bridgedResourceData)(options)).toMatch(/limit=10/);
});
test('with offset option', function () {
options.offset = 10;
expect((0, _bridgedresources.bridgedResourceData)(options)).toMatch(/offset=10/);
});
describe('values option', function () {
test('with one value', function () {
options.values = {
a: 'b'
};
expect((0, _bridgedresources.bridgedResourceData)(options)).toMatch(/values%5Ba%5D=b/);
});
test('with multiple values', function () {
options.values = {
a: 'b',
c: 'd'
};
var url = (0, _bridgedresources.bridgedResourceData)(options);
expect(url).toMatch(/values%5Ba%5D=b/);
expect(url).toMatch(/values%5Bc%5D=d/);
});
});
describe('metadata option', function () {
test('with one value', function () {
options.metadata = {
a: 'b'
};
expect((0, _bridgedresources.bridgedResourceData)(options)).toMatch(/metadata%5Ba%5D=b/);
});
test('with multiple values', function () {
options.metadata = {
a: 'b',
c: 'd'
};
var url = (0, _bridgedresources.bridgedResourceData)(options);
expect(url).toMatch(/metadata%5Ba%5D=b/);
expect(url).toMatch(/metadata%5Bc%5D=d/);
});
});
describe('attributes option', function () {
test('with one attribute', function () {
options.attributes = ['a'];
expect((0, _bridgedresources.bridgedResourceData)(options)).toMatch(/attributes=a/);
});
test('with multiple values', function () {
options.attributes = ['a', 'b'];
expect((0, _bridgedresources.bridgedResourceData)(options)).toMatch(/attributes=a%2Cb/);
});
});
});
describe('#count', function () {
describe('when successful', function () {
var response;
beforeEach(function () {
response = {
status: 200,
data: {
count: 2
}
};
_axios["default"].post = (0, _promises.resolvePromiseWith)(response);
});
test('does not return errors', function () {
expect.assertions(1);
return (0, _bridgedresources.countBridgedResource)(options).then(function (_ref) {
var error = _ref.error;
expect(error).toBeUndefined();
});
});
test('returns an object with a count', function () {
expect.assertions(1);
// eslint-disable-next-line
return (0, _bridgedresources.countBridgedResource)(options).then(function (_ref2) {
var count = _ref2.count;
expect(count).toBe(2);
});
});
});
});
describe('#fetch', function () {
describe('when successful', function () {
describe('multiple records', function () {
var response;
beforeEach(function () {
response = {
status: 200,
data: {
records: {
fields: ['Field A', 'Field B'],
records: [['Value A1', 'Value B1'], ['Value A2', 'Value B2']],
metadata: {
size: 2,
nextPageToken: null
}
}
}
};
_axios["default"].post = (0, _promises.resolvePromiseWith)(response);
});
test('does not return errors', function () {
expect.assertions(1);
return (0, _bridgedresources.fetchBridgedResource)(options).then(function (_ref3) {
var serverError = _ref3.serverError;
expect(serverError).toBeUndefined();
});
});
test('returns a records object', function () {
expect.assertions(7);
return (0, _bridgedresources.fetchBridgedResource)(options).then(function (_ref4) {
var records = _ref4.records;
expect(records).toBeDefined();
expect(records).toBeInstanceOf(Array);
expect(records).toHaveLength(2);
expect(records[0]).toHaveProperty('Field A', 'Value A1');
expect(records[0]).toHaveProperty('Field B', 'Value B1');
expect(records[1]).toHaveProperty('Field A', 'Value A2');
expect(records[1]).toHaveProperty('Field B', 'Value B2');
});
});
});
describe('single record', function () {
var response;
beforeEach(function () {
response = {
status: 200,
data: {
record: {
attributes: {
'Field A': 'Value A',
'Field B': 'Value B'
}
}
}
};
_axios["default"].post = (0, _promises.resolvePromiseWith)(response);
});
test('does not return errors', function () {
expect.assertions(1);
return (0, _bridgedresources.fetchBridgedResource)(options).then(function (_ref5) {
var error = _ref5.error;
expect(error).toBeUndefined();
});
});
test('returns a record object', function () {
expect.assertions(3);
return (0, _bridgedresources.fetchBridgedResource)(options).then(function (_ref6) {
var record = _ref6.record;
expect(record).toBeDefined();
expect(record).toHaveProperty('Field A');
expect(record).toHaveProperty('Field B');
});
});
});
});
});
});