Extract the generate_url(..) function from node.js, and collection.js

This commit is contained in:
Violet Cheng
2017-08-17 21:43:07 +05:30
committed by Ashesh Vashi
parent e9b80dae9c
commit d527769bf8
4 changed files with 147 additions and 71 deletions

View File

@@ -0,0 +1,95 @@
import {generate_url} from 'sources/browser/generate_url';
describe('generate_url', () => {
describe('in collection', () => {
let baseUrl, treeInfo, actionType, nodeType, pickFunction;
beforeEach(() => {
baseUrl = 'http://base/and-extension/';
treeInfo = {
treeNode1: {
_id: 'an_id',
priority: 1000,
},
};
actionType = 'actionType';
nodeType = 'nodeType';
pickFunction = () => {
return true;
};
});
it('returns a correctly formatted URL', () => {
let formattedUrl = generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction);
expect(formattedUrl).toEqual('http://base/and-extension/nodeType/actionType/an_id/');
});
describe('given there are multiple treeInfoItems', () => {
beforeEach(() => {
treeInfo['treeNode2'] = {
_id: 'another_id',
priority: 500,
};
treeInfo['treeNode3'] = {
_id: 'a_third_id',
priority: 100,
};
pickFunction = (value, key) => {
return key != 'treeNode2';
};
});
it('chooses the correct treeInfo', () => {
let formattedUrl = generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction);
expect(formattedUrl).toEqual('http://base/and-extension/nodeType/actionType/a_third_id/an_id/');
});
});
});
describe('in node', () => {
let baseUrl, treeInfo, actionType, nodeType, pickFunction, itemDataID;
beforeEach(() => {
baseUrl = 'http://base/and-extension/';
treeInfo = {
treeNode1: {
_id: 'an_id',
priority: 1000,
},
};
actionType = 'actionType';
nodeType = 'nodeType';
pickFunction = () => {
return true;
};
itemDataID = 'item1';
});
it('returns a correctly formatted URL', () => {
let formattedUrl = generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction, itemDataID);
expect(formattedUrl).toEqual('http://base/and-extension/nodeType/actionType/an_id/item1');
});
describe('given there are multiple treeInfoItems', () => {
beforeEach(() => {
treeInfo['treeNode2'] = {
_id: 'another_id',
priority: 500,
};
treeInfo['treeNode3'] = {
_id: 'a_third_id',
priority: 100,
};
pickFunction = (value) => {
return value.priority > 100;
};
});
it('chooses the correct treeInfo', () => {
let formattedUrl = generate_url(baseUrl, treeInfo, actionType, nodeType, pickFunction, itemDataID);
expect(formattedUrl).toEqual('http://base/and-extension/nodeType/actionType/another_id/an_id/item1');
});
});
});
});