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'); expect(obj.getCurrent()).toEqual('remote');
}); });
test('Store do not execute if empty', () => { test('Store is executed', async () => {
expect(obj.store({ item: 'test' })).toBeUndefined(); const spy = jest.spyOn(obj, '__exec');
await obj.store({ item: 'test' });
expect(spy).toBeCalledTimes(1);
}); });
test('Load default storages ', () => { test('Load default storages ', () => {
obj.loadDefaultProviders();
expect(obj.get('local')).toBeTruthy(); expect(obj.get('local')).toBeTruthy();
expect(obj.get('remote')).toBeTruthy(); expect(obj.get('remote')).toBeTruthy();
expect(obj.get('test')).toBeFalsy(); 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'; import RemoteStorage from 'storage_manager/model/RemoteStorage';
describe('LocalStorage', () => { describe('LocalStorage', () => {
var obj; let obj;
var itemName = 'testItem'; let data = {
var data = {
item1: 'value1', item1: 'value1',
item2: 'value2' item2: 'value2',
}; };
beforeEach(() => { beforeEach(() => {
@ -17,141 +16,84 @@ describe('LocalStorage', () => {
obj = null; obj = null;
}); });
test('Store and load items', () => { test('Store and load items', async () => {
obj.store(data); await obj.store(data);
var result = obj.load(['item1', 'item2']); const result = await obj.load();
expect(result).toEqual(data); 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', () => { describe('RemoteStorage', () => {
var obj; let obj;
var itemName = 'testItem'; let data;
var endpointStore = 'testStoreEndpoint'; let defaultOpts = {
var endpointLoad = 'testLoadEndpoint'; urlStore: '/store',
var params = { test: 'testValue' }; urlLoad: '/load',
var storageOptions; credentials: true,
var data; headers: { 'X-Requested-With': 'XMLHttpRequest' },
var mockResponse = (body = {}) => { };
let mockResponse = (body = {}) => {
return new window.Response(JSON.stringify(body), { return new window.Response(JSON.stringify(body), {
status: 200, status: 200,
headers: { 'Content-type': 'application/json' } headers: { 'Content-type': 'application/json' },
}); });
}; };
beforeEach(() => { beforeEach(() => {
data = { data = {
item1: 'value1', item1: 'value1',
item2: 'value2' item2: 'value2',
};
storageOptions = {
urlStore: endpointStore,
urlLoad: endpointLoad,
params
}; };
obj = new RemoteStorage(storageOptions); obj = new RemoteStorage();
sinon obj.request = jest.fn(() => Promise.resolve(mockResponse({ data: 1 })));
.stub(obj, 'fetch')
.returns(Promise.resolve(mockResponse({ data: 1 })));
}); });
afterEach(() => { afterEach(() => {
obj.fetch.restore(); obj.request.mockRestore();
obj = null; obj = null;
}); });
test('Store data', () => { test('Store data', async () => {
obj.store(data); await obj.store(data, defaultOpts);
const callResult = obj.fetch; const { calls } = obj.request.mock;
expect(callResult.called).toEqual(true); expect(calls.length).toBe(1);
expect(callResult.firstCall.args[0]).toEqual(endpointStore); expect(calls[0][0]).toBe(defaultOpts.urlStore);
}); // expect(obj.request).toBeCalledWith(opts.urlStore, defaultOpts, opts);
const { body, ...args } = calls[0][1];
test('Load data', () => { expect(args).toEqual({
obj.load(['item1', 'item2']); method: 'POST',
const callResult = obj.fetch; headers: defaultOpts.headers,
expect(callResult.called).toEqual(true); credentials: defaultOpts.credentials,
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 with credentials option as 'include' by default", () => { test('Load data', async () => {
obj.store(data); await obj.load(defaultOpts);
const callResult = obj.fetch; const { calls } = obj.request.mock;
expect(callResult.called).toEqual(true); expect(obj.request).toBeCalledTimes(1);
expect(callResult.firstCall.args[1]).toMatchObject({ expect(calls[0][0]).toBe(defaultOpts.urlLoad);
credentials: 'include' expect(calls[0][1]).toEqual({
method: 'GET',
body: undefined,
headers: defaultOpts.headers,
credentials: defaultOpts.credentials,
}); });
}); });
test('Store data with credentials option as false ', () => { test('Load data with custom fetch options', async () => {
obj = new RemoteStorage({ ...storageOptions, credentials: false }); const customOpts = { customOpt: 'customValue' };
sinon await obj.load({
.stub(obj, 'fetch') ...defaultOpts,
.returns(Promise.resolve(mockResponse({ data: 1 }))); fetchOptions: () => customOpts,
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 as function', () => { expect(obj.request).toBeCalledTimes(1);
const customOpts = { customOpt: 'customValue' }; expect(obj.request.mock.calls[0][1]).toEqual({
obj = new RemoteStorage({ method: 'GET',
...storageOptions, body: undefined,
fetchOptions: () => { headers: defaultOpts.headers,
return customOpts; 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