export const predicateDescribe = R.curry((pred, title, func) => { if (typeof pred != 'function'){ pred = R.always(pred); } if (pred()){ describe(title, func); } });
describe('Full app: 1 - Array', function() { describe('1.1 - #indexOf()', function() { return it('should return -1 when the value is not present', function() { expect([1, 2, 3].indexOf(5)).to.equal(-1); return expect([1, 2, 3].indexOf(0)).to.equal(-1); }); }); describe('1.2 - length', function() { return it('should return length of array', function() { return expect([1, 2, 3].length).to.equal(3); }); }); describe("Specify", function () { specify("it works", function () { expect(true).to.be.true; }); xspecify("Skip: This won't run", function () { throw new Error("This won't run") }) }) context("Context test", function () { it("it works", function () { expect(true).to.be.true; }); }); xcontext("Skip suite (xcontext)", function () { it("This won't run", function () { throw new Error("This won't run") }) }) });
describe('TreeNode', function () { describe('hasChildren', function () { it('should return true if the node has children', function () { const node = { type: 'textTreeNode', children: ['id1'], }; const treeNode = new TreeNode(node); expect(treeNode.hasChildren()).to.be.ok; }); it('should return false if the node has no children', function () { const node = { type: 'textTreeNode', children: undefined, }; const treeNode = new TreeNode(node); expect(treeNode.hasChildren()).to.be.not.ok; }); }); });
describe('StatList', function () { let stats; const defaultItems = { strength: 1, agility: 2, smarts: 3, spirit: 4, vigor: 1, }; function render(items = defaultItems) { const label = 'Attributes'; stats = shallow(<StatList items={items} label={label} />); } describe('Rendering', function () { render(); it('should show five DieStatLine children', function () { expect(stats.find(DieStatLine)).to.have.a.lengthOf(5); }); it('should show the label', function () { expect(stats.text()).to.contain('Attributes'); }); it('should pass correct values to DieStatLine', function () { const dieStatLines = stats.find(DieStatLine); const values = dieStatLines.map(line => line.prop('value')); expect(values[0]).to.equal(1); expect(values[1]).to.equal(2); expect(values[2]).to.equal(3); expect(values[3]).to.equal(4); expect(values[4]).to.equal(1); }); }); });
describe('Character methods', function () { describe('updateAttribute', function () { it('should update attribute', function () { const attribute = 'strength'; const expected = 3; const id = Characters.insert({ attributes: { [attribute]: 0 } }); Meteor.call('Characters.updateAttribute', id, attribute, expected); const actual = Characters.findOne(id).attributes[attribute]; expect(expected).to.be.equal(actual); }); it('should not delete other attributes', function () { const id = Characters.insert({ attributes: { strength: 0, agility: 0 } }); Meteor.call('Characters.updateAttribute', id, 'strength', 1); Meteor.call('Characters.updateAttribute', id, 'agility', 1); const attributes = Characters.findOne(id).attributes; expect(attributes).to.contain.key('strength'); expect(attributes).to.contain.key('agility'); }); }); describe('updateSkill', function () { it('should update skill', function () { const skill = 'throwing'; const expected = 3; const id = Characters.insert({ skills: { [skill]: 0 } }); Meteor.call('Characters.updateSkill', id, skill, expected); const actual = Characters.findOne(id).skills[skill]; expect(expected).to.be.equal(actual); }); it('should not delete other attributes', function () { const id = Characters.insert({ skills: { throwing: 0, fighting: 0 } }); Meteor.call('Characters.updateSkill', id, 'throwing', 1); Meteor.call('Characters.updateAttribute', id, 'fighting', 1); const skills = Characters.findOne(id).skills; expect(skills).to.contain.key('throwing'); expect(skills).to.contain.key('fighting'); }); }); describe('toggleSkill', function () { context('when skill is not present', function () { const skill = 'throwing'; const id = Characters.insert({ skills: {} }); Meteor.call('Characters.toggleSkill', id, skill); let skills = Characters.findOne(id).skills; it('should add the skill', function () { expect(skills).to.contain.key(skill); }); it('should set the value to 1', function () { expect(skills[skill]).to.equal(1); }); context('when there are multiple calls', function () { const skillTwo = 'climbing'; const skillThree = 'fighting'; Meteor.call('Characters.toggleSkill', id, skillTwo); Meteor.call('Characters.toggleSkill', id, skillThree); skills = Characters.findOne(id).skills; it('should not replace skills object', function () { expect(skills).to.contain.key(skillTwo); expect(skills).to.contain.key(skillThree); }); }); }); context('when skill is present', function () { it('should call update to remove skill', function () { const skill = 'throwing'; const id = Characters.insert({ skills: { [skill]: 3, fighting: 5 } }); Meteor.call('Characters.toggleSkill', id, skill); const skills = Characters.findOne(id).skills; expect(skills).to.not.contain.key(skill); }); }); }); });
describe('1 - Array', function() { describe('1.1 - #indexOf()', function() { return it('should return -1 when the value is not present', function() { expect([1, 2, 3].indexOf(5)).to.equal(-1); return expect([1, 2, 3].indexOf(0)).to.equal(-1); }); }); describe('1.2 - length', function() { return it('should return length of array', function() { return expect([1, 2, 3].length).to.equal(3); }); }); describe("Specify", function () { specify("it works", function () { expect(true).to.be.true; }); xspecify("Skip: This won't run", function () { throw new Error("This won't run") }) }) context("Context test", function () { it("it works", function () { expect(true).to.be.true; }); }); xcontext("Skip suite (xcontext)", function () { it("This won't run", function () { throw new Error("This won't run") }) }) });
describe("Login method facebook-native", ()=>{ beforeEach(()=>{ this.loginHandler = new NativeFacebookLoginHandler(); this.accessToken = "EAADQ34PLCsYBACZBQUY6TgQvqjuc2Ng2Z"; this.options = { methodName: NativeFacebookLoginHandler.METHOD_NAME, accessToken: this.accessToken, userID: "1231234123", expiresIn: "38123" }; sinon.stub(HTTP, "get").returns({data: {}}); sinon.stub(this.loginHandler, "getIdentity").returns({}); }); afterEach(()=>{ HTTP.get.restore(); // Since in restoring the stub in getIdentity() suite make sure it exists before calling it. let restore = this.loginHandler.getIdentity.restore; restore && restore.call(this.loginHandler.getIdentity); }); describe("getIdentity()", ()=>{ beforeEach(()=>{ this.loginHandler.getIdentity.restore(); }); it("should call facebook to get user public fields", ()=>{ this.loginHandler.getIdentity(this.accessToken); expect(HTTP.get).to.have.been.calledWith(this.loginHandler._apiUri, { params: { access_token: accessToken, fields: this.loginHandler._fields } }); }); }); it("return undefined if it's not facebook-native login method", ()=>{ let value = this.loginHandler.login({ methodName: "login" }); expect(value).to.equal(undefined); }); xit("should search for Meteor user", ()=>{ sinon.stub(Meteor.users, "findOne"); this.loginHandler.login(this.options); expect(Meteor.users.findOne).to.have.been.calledWith({ "services.facebook.id": this.options.userID }); Meteor.users.findOne.restore() }); it("get user default fields if user don't exists", ()=> { this.loginHandler.login(this.options); expect(this.loginHandler.getIdentity).to.have.been.calledWith(this.accessToken); }); });
describe('meteorInstallHot', () => { it('fails if no relevant hot.accept() found', () => { const s = new Sandbox(); s.exec(` var require = meteorInstall({ client: { "noHotAccept.js": function(require, exports, module) { exports.testValue = 1; } } }); require('/client/noHotAccept.js').testValue; `, 'noHotAccept1.js').should.equal(1); // it loads properly s.exec(` meteorInstallHot({ client: { "noHotAccept.js": [function(require, exports, module) { }] } }); hot.failedOnce; `, 'noHotAccept2.js').should.equal(true); // No relevant hot.accept() found }); it('can self accept', () => { const s = new Sandbox(); s.exec(` var testValue; var require = meteorInstall({ client: { "selfAcceptTest.js": function(require, exports, module) { module.hot.accept(); testValue = 1; } } }); require('/client/selfAcceptTest.js'); testValue; `, 'selfAcceptTest1.js').should.equal(1); s.exec(` meteorInstallHot({ client: { "selfAcceptTest.js": [function(require, exports, module) { testValue = 2; }] } }); testValue; `, 'selfAcceptTest1.js').should.equal(2); s.exec('hot.failedOnce').should.be.false; }); it('can accept a dep and receives new exports', () => { const s = new Sandbox(); s.exec(` var testValue; var require = meteorInstall({ client: { "test2.js": function(require, exports, module) { exports.testValue = 1; }, "test1.js": ['./test2.js', function(require, exports, module) { testValue = require('./test2.js').testValue; module.hot.accept('./test2.js', function() { testValue = require('./test2.js').testValue; }); }] } }); require('/client/test1.js'); testValue; `, 'replacesExportsSetup.js').should.equal(1); s.exec(` meteorInstallHot({ client: { "test2.js": function(require, exports, module) { exports.testValue = 2; } } }); testValue; `, 'replacesExportsTest.js').should.equal(2); s.exec('hot.failedOnce').should.be.false; }); // hot.accept("hot-test") and update "hot-test/lib/index.js" it('node_modules with { main: "./lib" }', () => { const s = new Sandbox(); s.exec(` var testValue; var require = meteorInstall({ client: { "app.js": ['hot-test', function(require, exports, module) { testValue = require('hot-test').test; module.hot.accept('hot-test', function() { testValue = require('hot-test').test; }); }] }, node_modules: { "hot-test": { "package.json": function(require, exports) { exports.main = "./lib"; }, lib: { "index.js": function(require, exports) { exports.test = 1; } } } } }); require('/client/app.js'); testValue; `, 'nodeModuleSetup.js').should.equal(1); s.exec(` meteorInstallHot({ node_modules: { "hot-test": { lib: { "index.js": function(require, exports) { exports.test = 2; } } } } }); testValue; `, 'nodeModuleTest.js').should.equal(2); s.exec('hot.failedOnce').should.be.false; }); it('node_modules with { main: "main.js" } (in root directory)', () => { const s = new Sandbox(); s.exec(` var testValue; var require = meteorInstall({ client: { "app.js": ['hot-test', function(require, exports, module) { testValue = require('hot-test').test; module.hot.accept('hot-test', function() { testValue = require('hot-test').test; }); }] }, node_modules: { "hot-test": { "package.json": function(require, exports) { exports.main = "main.js"; }, "main.js": function(require, exports) { exports.test = 1; } } } }); require('/client/app.js'); testValue; `, 'nodeModuleSetup.js').should.equal(1); s.exec(` meteorInstallHot({ node_modules: { "hot-test": { "main.js": function(require, exports) { exports.test = 2; } } } }); testValue; `, 'nodeModuleTest.js').should.equal(2); s.exec('hot.failedOnce').should.be.false; }); });
describe('TreeNodes Collection', function () { let tree = []; beforeEach(function () { StubCollections.stub(TreeNodes); tree = []; createTree('testTree', 2, 2, tree); TreeNodes.remove({}); tree.forEach(node => { TreeNodes.insert(node); }); }); describe('TreeNodes.getRoot(treeId)', function () { it('should return falsy if the treeId doesnt exist', function () { expect(TreeNodes.getRoot('nonExistingTreeId')).to.be.not.ok; }); it('should get the root node for a given treeId', function () { expect(TreeNodes.getRoot('testTree')).to.deep.equal(tree[0]); }); }); describe('TreeNodes.getContentFromRootTo(treeId, nodeId)', function () { it(`should return the content of all nodes from root to the given one in order`, function () { const expectedForRoot11 = ['root', 'root1', 'root11']; const expectedForRoot10 = ['root', 'root1', 'root10']; const expectedForRoot01 = ['root', 'root0', 'root01']; const expectedForRoot00 = ['root', 'root0', 'root00']; const expectedForRoot1 = ['root', 'root1']; const expectedForRoot0 = ['root', 'root0']; const expectedForRoot = ['root']; expect(TreeNodes.getContentFromRootTo('testTree', 'root11')) .to.deep.equal(expectedForRoot11); expect(TreeNodes.getContentFromRootTo('testTree', 'root10')) .to.deep.equal(expectedForRoot10); expect(TreeNodes.getContentFromRootTo('testTree', 'root01')) .to.deep.equal(expectedForRoot01); expect(TreeNodes.getContentFromRootTo('testTree', 'root00')) .to.deep.equal(expectedForRoot00); expect(TreeNodes.getContentFromRootTo('testTree', 'root1')) .to.deep.equal(expectedForRoot1); expect(TreeNodes.getContentFromRootTo('testTree', 'root0')) .to.deep.equal(expectedForRoot0); expect(TreeNodes.getContentFromRootTo('testTree', 'root')) .to.deep.equal(expectedForRoot); }); it('should return empty content if the treeId or the nodeId dont exist', function () { expect(TreeNodes.getContentFromRootTo('nonExistingTreeId', 'nonNodeId')) .to.deep.equal([]); }); }); describe('TreeNodes.getNodesFromRootTo(treeId, nodeId)', function () { it('should return all nodes from root to the given one in order', function () { const expectedForRoot11 = ['root', 'root1', 'root11']; const expectedForRoot10 = ['root', 'root1', 'root10']; const expectedForRoot01 = ['root', 'root0', 'root01']; const expectedForRoot00 = ['root', 'root0', 'root00']; const expectedForRoot1 = ['root', 'root1']; const expectedForRoot0 = ['root', 'root0']; const expectedForRoot = ['root']; expect(TreeNodes.getNodesFromRootTo('testTree', 'root11').map(n => n._id)) .to.deep.equal(expectedForRoot11); expect(TreeNodes.getNodesFromRootTo('testTree', 'root10').map(n => n._id)) .to.deep.equal(expectedForRoot10); expect(TreeNodes.getNodesFromRootTo('testTree', 'root01').map(n => n._id)) .to.deep.equal(expectedForRoot01); expect(TreeNodes.getNodesFromRootTo('testTree', 'root00').map(n => n._id)) .to.deep.equal(expectedForRoot00); expect(TreeNodes.getNodesFromRootTo('testTree', 'root1').map(n => n._id)) .to.deep.equal(expectedForRoot1); expect(TreeNodes.getNodesFromRootTo('testTree', 'root0').map(n => n._id)) .to.deep.equal(expectedForRoot0); expect(TreeNodes.getNodesFromRootTo('testTree', 'root').map(n => n._id)) .to.deep.equal(expectedForRoot); }); it('should return empty content if the treeId or the nodeId dont exist', function () { expect(TreeNodes.getNodesFromRootTo('nonExistingTreeId', 'nonNodeId')) .to.deep.equal([]); }); }); describe('TreeNodes.count', function () { it('should return the number of nodes of given tree', function () { expect(TreeNodes.count('testTree')).to.equal(7); }); it('should return 0 if the tree does not exist or have no nodes', function () { expect(TreeNodes.count('nonExistingTree')).to.equal(0); }); }); describe('TreeNodes.getTree(treeId, tillLevel)', function () { it('should return undefined if the tree does not exist', function () { const treeObject = TreeNodes.getTree('nonExistingTree'); expect(treeObject).to.equal(undefined); }); it('should return all nodes inside a tree structure', function () { const treeObject = TreeNodes.getTree('testTree'); expect(treeObject).to.deep.equals({ root: { _id: 'root', content: 'root', level: 0, parent: null, children: { root0: { _id: 'root0', content: 'root0', level: 1, parent: 'root', children: { root00: { _id: 'root00', content: 'root00', level: 2, parent: 'root0', children: {}, }, root01: { _id: 'root01', content: 'root01', level: 2, parent: 'root0', children: {}, }, }, }, root1: { _id: 'root1', content: 'root1', level: 1, parent: 'root', children: { root10: { _id: 'root10', content: 'root10', level: 2, children: {}, parent: 'root1', }, root11: { _id: 'root11', content: 'root11', level: 2, children: {}, parent: 'root1', }, }, }, }, }, }); }); }); describe('TreeNodes.getNodesFromLevel(treeId, level)', function () { it('should return all the nodes for the given level for the given tree', function () { const expectedLevel0 = ['root']; const expectedLevel1 = ['root0', 'root1']; const expectedLevel2 = ['root00', 'root01', 'root10', 'root11']; expect(TreeNodes.getNodesFromLevel('testTree', 0).map(n => n._id)) .to.deep.equal(expectedLevel0); expect(TreeNodes.getNodesFromLevel('testTree', 1).map(n => n._id)) .to.include.members(expectedLevel1); expect(TreeNodes.getNodesFromLevel('testTree', 2).map(n => n._id)) .to.include.members(expectedLevel2); expect(TreeNodes.getNodesFromLevel('testTree', 3).map(n => n._id)) .to.deep.equal([]); }); }); describe('TreeNodes.createTree(treeId, content)', function () { it('should create a new root node for a new tree, returning the nodeId', function () { const rootId = TreeNodes.createTree('anotherTree', 'testContent'); const rootNode = TreeNodes.findOne(rootId); expect(rootNode).to.deep.equals({ _id: rootId, treeId: 'anotherTree', level: 0, content: 'testContent', parent: null, }); }); }); describe('TreeNodes.appendChild(treeId, parentId, content)', function () { it(`should insert a new node adding automatically the level, returning the nodeId The parent should have the new child id inside its children array`, function () { const content = 'hello'; const nodeId = TreeNodes.appendChild('testTree', 'root', content); const node = TreeNodes.findOne(nodeId); expect(node).to.deep.equals({ _id: nodeId, treeId: 'testTree', level: 1, content, parent: 'root', }); const parent = TreeNodes.findOne('root'); expect(parent.children).to.include(nodeId); }); it('should return falsy if the parent node or the tree doesnt exist', function () { expect(TreeNodes.appendChild()).to.be.not.ok; }); }); afterEach(function () { TreeNodes.remove({}); StubCollections.restore(); }); });