UNPKG

@kineticdata/react

Version:
554 lines (553 loc) 17.4 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; var _axios = _interopRequireDefault(require("axios")); var _submissions = require("./submissions"); var _promises = require("../../../tests/utils/promises"); // Mock out the bundle object from a dependency. jest.mock('../../helpers', function () { return { bundle: { spaceLocation: function spaceLocation() { return 'mock-space'; }, apiLocation: function apiLocation() { return 'mock-space/app/api/v1'; }, kappSlug: function kappSlug() { return 'mock-kapp'; } } }; }); describe('SubmissionSearch', function () { var search; beforeEach(function () { search = new _submissions.SubmissionSearch(); }); test('empty searchers have no query', function () { expect(search.build().query).toEqual(''); }); describe('#eq', function () { test('eq adds an equality comparison', function () { expect(search.eq('attr', 'val').build().query).toEqual('attr = "val"'); }); test('eq assumes null for empty rvalue', function () { expect(search.eq('attr', '').build().query).toEqual('attr = null'); }); test('multiple eq implies an and', function () { expect(search.eq('val1', '1').eq('val2', '2').build().query).toEqual('val1 = "1" AND val2 = "2"'); }); }); describe('#gt', function () { beforeEach(function () { search.setDatastore(true); }); test('cannot be used with kapps', function () { search.setDatastore(false); expect(function () { search.gt('foo', 'bar'); }).toThrow(); }); test('adds a greater-than comparison', function () { expect(search.gt('attr', 'val').build().query).toEqual('attr > "val"'); }); }); describe('#lt', function () { beforeEach(function () { search.setDatastore(true); }); test('cannot be used with kapps', function () { search.setDatastore(false); expect(function () { search.lt('foo', 'bar'); }).toThrow(); }); test('adds a less-than comparison', function () { expect(search.lt('attr', 'val').build().query).toEqual('attr < "val"'); }); }); describe('#gteq', function () { beforeEach(function () { search.setDatastore(true); }); test('cannot be used with kapps', function () { search.setDatastore(false); expect(function () { search.gteq('foo', 'bar'); }).toThrow(); }); test('adds a greater-than-or-equal comparison', function () { expect(search.gteq('attr', 'val').build().query).toEqual('attr >= "val"'); }); }); describe('#lteq', function () { beforeEach(function () { search.setDatastore(true); }); test('cannot be used with kapps', function () { search.setDatastore(false); expect(function () { search.lteq('foo', 'bar'); }).toThrow(); }); test('adds a less-than-or-equal comparison', function () { expect(search.lteq('attr', 'val').build().query).toEqual('attr <= "val"'); }); }); describe('#between', function () { beforeEach(function () { search.setDatastore(true); }); test('cannot be used with kapps', function () { search.setDatastore(false); expect(function () { search.between('foo', 'bar'); }).toThrow(); }); test('adds a between comparison', function () { expect(search.between('attr', 'val1', 'val2').build().query).toEqual('attr BETWEEN ("val1", "val2")'); }); }); describe('#in', function () { test('in generates an in-list', function () { expect(search["in"]('attr', ['val1', 'val2']).build().query).toEqual('attr IN ("val1", "val2")'); }); test('in assumes null for empty rvalue', function () { expect(search["in"]('attr', ['val1', '']).build().query).toEqual('attr IN ("val1", null)'); }); test('in handles null for rvalue', function () { expect(search["in"]('attr', ['val1', null]).build().query).toEqual('attr IN ("val1", null)'); }); }); describe('groupings', function () { test('#or cannot be used with datastore', function () { search.setDatastore(true); expect(function () { search.or(); }).toThrow(); }); test('or separates equalities in its context with OR', function () { expect(search.or().eq('a', '1').eq('b', '2').end().build().query).toEqual('( a = "1" OR b = "2")'); }); test('or following other equalities implies an and', function () { expect(search.eq('out', 'outer').or().eq('a', '1').eq('b', '2').end().build().query).toEqual('out = "outer" AND ( a = "1" OR b = "2")'); }); test('complex query', function () { expect(search.eq('always', 'needed').or().eq('a', 'toBeA').and().eq('b', 'toBeB').eq('c', 'toBeC').end().end().build().query).toEqual('always = "needed" AND ( a = "toBeA" OR ( b = "toBeB" AND c = "toBeC"))'); }); }); describe('search metadata', function () { describe('#type', function () { test('sets the type', function () { expect(search.type('foo').build().type).toBe('foo'); }); test('cannot be used with datastore', function () { search.setDatastore(true); expect(function () { search.type('foo'); }).toThrow(); }); }); describe('#coreState', function () { test('sets coreState', function () { expect(search.coreState('Closed').build().coreState).toBe('Closed'); }); test('setting an invalid coreState throws an error', function () { expect(function () { search.coreState('InvalidCoreState'); }).toThrow(); }); test('datastore cannot be closed', function () { search.setDatastore(true); expect(function () { search.coreState('Closed'); }).toThrow(); }); }); describe('#startDate', function () { test('cannot be used with datastore', function () { search.setDatastore(true); expect(function () { search.startDate(new Date()); }).toThrow(); }); test('throws an error if it is not a valid date', function () { expect(function () { search.startDate(1); }).toThrow(); }); test('sets the date as an ISO string', function () { expect(search.startDate(new Date()).build().start).toEqual(expect.stringMatching(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/)); }); test('throws an error if used inside a query scope', function () { expect(function () { search.or().startDate(new Date()); }).toThrow(); }); }); describe('#endDate', function () { test('cannot be used with datastore', function () { search.setDatastore(true); expect(function () { search.endDate(new Date()); }).toThrow(); }); test('throws an error if it is not a valid date', function () { expect(function () { search.endDate(1); }).toThrow(); }); test('sets the date as an ISO string', function () { expect(search.startDate(new Date()).build().start).toEqual(expect.stringMatching(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/)); }); test('throws an error if used inside a query scope', function () { expect(function () { search.or().endDate(new Date()); }).toThrow(); }); }); describe('build-time validations', function () { test('defaults end date to now if start is set and end is omitted', function () {}); test('validates start date is before end', function () {}); }); }); }); describe('#searchSubmissions', function () { describe('when successful', function () { var response; var search; beforeEach(function () { response = { status: 200, data: { submissions: [], messages: [], nextPageToken: 'page-token' } }; search = new _submissions.SubmissionSearch().build(); _axios["default"].post = (0, _promises.resolvePromiseWith)(response); }); test('does not return errors', function () { expect.assertions(1); return (0, _submissions.searchSubmissions)({ search: search }).then(function (_ref) { var serverError = _ref.serverError; expect(serverError).toBeUndefined(); }); }); test('does return messages', function () { expect.assertions(2); return (0, _submissions.searchSubmissions)({ search: search }).then(function (_ref2) { var messages = _ref2.messages; expect(messages).toBeDefined(); expect(messages).toBeInstanceOf(Array); }); }); test('does return nextPageToken', function () { expect.assertions(1); return (0, _submissions.searchSubmissions)({ search: search }).then(function (_ref3) { var nextPageToken = _ref3.nextPageToken; expect(nextPageToken).toBeDefined(); }); }); test('does return submissions', function () { expect.assertions(2); return (0, _submissions.searchSubmissions)({ search: search }).then(function (_ref4) { var submissions = _ref4.submissions; expect(submissions).toBeDefined(); expect(submissions).toBeInstanceOf(Array); }); }); }); describe('when successful - get', function () { var response; var search; beforeEach(function () { response = { status: 200, data: { submissions: [], messages: [], nextPageToken: 'page-token' } }; search = new _submissions.SubmissionSearch().build(); _axios["default"].get = (0, _promises.resolvePromiseWith)(response); }); test('does return submissions', function () { expect.assertions(2); return (0, _submissions.searchSubmissions)({ search: search, get: true }).then(function (_ref5) { var submissions = _ref5.submissions; expect(submissions).toBeDefined(); expect(submissions).toBeInstanceOf(Array); }); }); }); }); describe('#fetchSubmission', function () { describe('when successful', function () { var id = 'abc123'; var values = { foo: 'bar' }; beforeEach(function () { _axios["default"].get = (0, _promises.resolvePromiseWith)({ status: 200, data: { submission: { id: id, values: values } } }); }); test('does not return errors', function () { expect.assertions(1); return (0, _submissions.fetchSubmission)({ id: id }).then(function (_ref6) { var errors = _ref6.errors; expect(errors).toBeUndefined(); }); }); test('returns a submission', function () { expect.assertions(1); return (0, _submissions.fetchSubmission)({ id: id }).then(function (_ref7) { var submission = _ref7.submission; expect(submission).toMatchObject({ id: id, values: values }); }); }); }); describe('when unsuccessful', function () { beforeEach(function () { _axios["default"].get = (0, _promises.rejectPromiseWith)({ response: { status: 500, statusText: 'Server Error' } }); }); test('throws an exception when no submission id is provided', function () { expect(function () { (0, _submissions.fetchSubmission)({}); }).toThrow(); }); test('does return errors', function () { expect.assertions(1); return (0, _submissions.fetchSubmission)({ id: 'fake' }).then(function (_ref8) { var error = _ref8.error; expect(error).toEqual({ statusCode: 500, key: null, message: 'Server Error' }); }); }); }); }); describe('#createSubmission', function () { var id = 'abc123'; var kappSlug = 'catalog'; var formSlug = 'ipad-request'; var values = { foo: 'bar' }; describe('when successful', function () { beforeEach(function () { _axios["default"].post = (0, _promises.resolvePromiseWith)({ status: 200, data: { submission: { id: id, values: values } } }); }); test('does not return errors', function () { expect.assertions(1); return (0, _submissions.createSubmission)({ kappSlug: kappSlug, formSlug: formSlug, values: values }).then(function (_ref9) { var errors = _ref9.errors; expect(errors).toBeUndefined(); }); }); test('returns a submission', function () { expect.assertions(1); return (0, _submissions.createSubmission)({ kappSlug: kappSlug, formSlug: formSlug, values: values }).then(function (_ref10) { var submission = _ref10.submission; expect(submission).toMatchObject({ id: id, values: values }); }); }); test('test defaults (kapp = bundle.kappSlug(), complete = true)', function () { expect.assertions(1); return (0, _submissions.createSubmission)({ formSlug: formSlug, values: values }).then(function () { expect(_axios["default"].post).toHaveBeenCalledWith('mock-space/app/api/v1/kapps/mock-kapp/forms/ipad-request/submissions', { values: values }, { params: { completed: true }, headers: { 'X-Kinetic-AuthAssumed': 'true' } }); }); }); test('test draft submission', function () { expect.assertions(1); return (0, _submissions.createSubmission)({ kappSlug: kappSlug, formSlug: formSlug, values: values, completed: false }).then(function () { expect(_axios["default"].post).toHaveBeenCalledWith('mock-space/app/api/v1/kapps/catalog/forms/ipad-request/submissions', { values: values }, { params: { completed: false }, headers: { 'X-Kinetic-AuthAssumed': 'true' } }); }); }); }); describe('when unsuccessful', function () { beforeEach(function () { _axios["default"].post = (0, _promises.rejectPromiseWith)({ response: { status: 500, statusText: 'Server Error' } }); }); test('throws an exception when no formSlug is provided', function () { expect(function () { (0, _submissions.createSubmission)({}); }).toThrow('createSubmission failed! The option "formSlug" is required.'); }); test('throws an exception when no values object is provided', function () { expect(function () { (0, _submissions.createSubmission)({ formSlug: formSlug }); }).toThrow('createSubmission failed! The option "values" is required.'); }); test('does return errors', function () { expect.assertions(1); return (0, _submissions.createSubmission)({ formSlug: formSlug, values: values }).then(function (_ref11) { var error = _ref11.error; expect(error).toEqual({ statusCode: 500, key: null, message: 'Server Error' }); }); }); }); }); describe('#deleteSubmission', function () { describe('when successful', function () { var id = 'abc123'; var values = { foo: 'bar' }; beforeEach(function () { _axios["default"]["delete"] = (0, _promises.resolvePromiseWith)({ status: 200, data: { submission: { id: id, values: values } } }); }); test('does not return errors', function () { expect.assertions(1); return (0, _submissions.deleteSubmission)({ id: id }).then(function (_ref12) { var errors = _ref12.errors; expect(errors).toBeUndefined(); }); }); test('returns a submission', function () { expect.assertions(1); return (0, _submissions.deleteSubmission)({ id: id }).then(function (_ref13) { var submission = _ref13.submission; expect(submission).toMatchObject({ id: id, values: values }); }); }); }); describe('when unsuccessful', function () { beforeEach(function () { _axios["default"]["delete"] = (0, _promises.rejectPromiseWith)({ response: { status: 500, statusText: 'Server Error' } }); }); test('throws an exception when no submission id is provided', function () { expect(function () { (0, _submissions.deleteSubmission)({}); }).toThrow(); }); test('does return errors', function () { expect.assertions(1); return (0, _submissions.deleteSubmission)({ id: 'fake' }).then(function (_ref14) { var error = _ref14.error; expect(error).toEqual({ statusCode: 500, key: null, message: 'Server Error' }); }); }); }); });