Browse Source

Update storage tests

pull/4223/head
Artur Arseniev 4 years ago
parent
commit
3cc3ada6e3
  1. 7
      test/specs/storage_manager/index.js
  2. 164
      test/specs/storage_manager/model/Models.js

7
test/specs/storage_manager/index.js

@ -48,12 +48,13 @@ describe('Storage Manager', () => {
expect(obj.getCurrent()).toEqual('remote');
});
test('Store do not execute if empty', () => {
expect(obj.store({ item: 'test' })).toBeUndefined();
test('Store is executed', async () => {
const spy = jest.spyOn(obj, '__exec');
await obj.store({ item: 'test' });
expect(spy).toBeCalledTimes(1);
});
test('Load default storages ', () => {
obj.loadDefaultProviders();
expect(obj.get('local')).toBeTruthy();
expect(obj.get('remote')).toBeTruthy();
expect(obj.get('test')).toBeFalsy();

164
test/specs/storage_manager/model/Models.js

@ -2,11 +2,10 @@ import LocalStorage from 'storage_manager/model/LocalStorage';
import RemoteStorage from 'storage_manager/model/RemoteStorage';
describe('LocalStorage', () => {
var obj;
var itemName = 'testItem';
var data = {
let obj;
let data = {
item1: 'value1',
item2: 'value2'
item2: 'value2',
};
beforeEach(() => {
@ -17,141 +16,84 @@ describe('LocalStorage', () => {
obj = null;
});
test('Store and load items', () => {
obj.store(data);
var result = obj.load(['item1', 'item2']);
test('Store and load items', async () => {
await obj.store(data);
const result = await obj.load();
expect(result).toEqual(data);
});
test('Store, update and load items', () => {
obj.store(data);
obj.store({ item3: 'value3' });
obj.store({ item2: 'value22' });
var result = obj.load(['item1', 'item2', 'item3']);
expect(result).toEqual({
item1: 'value1',
item2: 'value22',
item3: 'value3'
});
});
test('Remove items', () => {
var items = ['item1', 'item2', 'item3'];
obj.store(data);
obj.remove(items);
expect(obj.load(items)).toEqual({});
});
});
describe('RemoteStorage', () => {
var obj;
var itemName = 'testItem';
var endpointStore = 'testStoreEndpoint';
var endpointLoad = 'testLoadEndpoint';
var params = { test: 'testValue' };
var storageOptions;
var data;
var mockResponse = (body = {}) => {
let obj;
let data;
let defaultOpts = {
urlStore: '/store',
urlLoad: '/load',
credentials: true,
headers: { 'X-Requested-With': 'XMLHttpRequest' },
};
let mockResponse = (body = {}) => {
return new window.Response(JSON.stringify(body), {
status: 200,
headers: { 'Content-type': 'application/json' }
headers: { 'Content-type': 'application/json' },
});
};
beforeEach(() => {
data = {
item1: 'value1',
item2: 'value2'
};
storageOptions = {
urlStore: endpointStore,
urlLoad: endpointLoad,
params
item2: 'value2',
};
obj = new RemoteStorage(storageOptions);
sinon
.stub(obj, 'fetch')
.returns(Promise.resolve(mockResponse({ data: 1 })));
obj = new RemoteStorage();
obj.request = jest.fn(() => Promise.resolve(mockResponse({ data: 1 })));
});
afterEach(() => {
obj.fetch.restore();
obj.request.mockRestore();
obj = null;
});
test('Store data', () => {
obj.store(data);
const callResult = obj.fetch;
expect(callResult.called).toEqual(true);
expect(callResult.firstCall.args[0]).toEqual(endpointStore);
});
test('Load data', () => {
obj.load(['item1', 'item2']);
const callResult = obj.fetch;
expect(callResult.called).toEqual(true);
expect(callResult.firstCall.args[0]).toEqual(endpointLoad);
});
test("Load data with credentials option as 'include' by default", () => {
obj.load(['item1', 'item2']);
const callResult = obj.fetch;
expect(callResult.called).toEqual(true);
expect(callResult.firstCall.args[1]).toMatchObject({
credentials: 'include'
test('Store data', async () => {
await obj.store(data, defaultOpts);
const { calls } = obj.request.mock;
expect(calls.length).toBe(1);
expect(calls[0][0]).toBe(defaultOpts.urlStore);
// expect(obj.request).toBeCalledWith(opts.urlStore, defaultOpts, opts);
const { body, ...args } = calls[0][1];
expect(args).toEqual({
method: 'POST',
headers: defaultOpts.headers,
credentials: defaultOpts.credentials,
});
});
test("Store data with credentials option as 'include' by default", () => {
obj.store(data);
const callResult = obj.fetch;
expect(callResult.called).toEqual(true);
expect(callResult.firstCall.args[1]).toMatchObject({
credentials: 'include'
test('Load data', async () => {
await obj.load(defaultOpts);
const { calls } = obj.request.mock;
expect(obj.request).toBeCalledTimes(1);
expect(calls[0][0]).toBe(defaultOpts.urlLoad);
expect(calls[0][1]).toEqual({
method: 'GET',
body: undefined,
headers: defaultOpts.headers,
credentials: defaultOpts.credentials,
});
});
test('Store data with credentials option as false ', () => {
obj = new RemoteStorage({ ...storageOptions, credentials: false });
sinon
.stub(obj, 'fetch')
.returns(Promise.resolve(mockResponse({ data: 1 })));
obj.store(data);
const callResult = obj.fetch;
expect(callResult.called).toEqual(true);
expect(callResult.firstCall.args[1]).toMatchObject({
credentials: false
});
});
test('Load data with credentials option as false', () => {
obj = new RemoteStorage({ ...storageOptions, credentials: false });
sinon
.stub(obj, 'fetch')
.returns(Promise.resolve(mockResponse({ data: 1 })));
obj.load(['item1', 'item2']);
const callResult = obj.fetch;
expect(callResult.called).toEqual(true);
expect(callResult.firstCall.args[1]).toMatchObject({
credentials: false
test('Load data with custom fetch options', async () => {
const customOpts = { customOpt: 'customValue' };
await obj.load({
...defaultOpts,
fetchOptions: () => customOpts,
});
});
test('Load data with custom fetch options as function', () => {
const customOpts = { customOpt: 'customValue' };
obj = new RemoteStorage({
...storageOptions,
fetchOptions: () => {
return customOpts;
}
expect(obj.request).toBeCalledTimes(1);
expect(obj.request.mock.calls[0][1]).toEqual({
method: 'GET',
body: undefined,
headers: defaultOpts.headers,
credentials: defaultOpts.credentials,
...customOpts,
});
sinon
.stub(obj, 'fetch')
.returns(Promise.resolve(mockResponse({ data: 1 })));
obj.load(['item1', 'item2']);
const callResult = obj.fetch;
expect(callResult.called).toEqual(true);
expect(callResult.firstCall.args[1]).toMatchObject(customOpts);
});
});

Loading…
Cancel
Save