How to use getcontextname method in tox

Best Python code snippet using tox_python

subscribe.spec.js

Source:subscribe.spec.js Github

copy

Full Screen

1describe('subscribe() ', () => {2 let initialContexts;34 before(async () => {5 await coreReady;67 initialContexts = await Promise.all(8 glue.contexts.all().map((contextName) => {9 const context = glue.contexts.get(contextName);10 return { name: contextName, context };11 })12 );1314 return Promise.all(glue.contexts.all().map((context) => glue.contexts.destroy(context)));15 });1617 afterEach(() => {18 gtf.clearWindowActiveHooks();1920 return Promise.all(glue.contexts.all().map((context) => glue.contexts.destroy(context)));21 });2223 after(() => {24 return Promise.all(25 initialContexts.map(({ name, context }) => {26 glue.contexts.update(name, context);27 })28 );29 });3031 describe('when manipulated by current app, ', function () {32 it('should throw when no arguments are passed', (done) => {33 try {34 glue.contexts.subscribe();35 done('should throw an error');36 } catch (error) {37 done();38 }39 });4041 [undefined, null, false, true, '', 42, [], { tick: 42 }].forEach((invalidName) => {42 it(`should throw when an invalid name (${JSON.stringify(invalidName)}) is passed`, (done) => {43 try {44 glue.contexts.subscribe(invalidName, () => {});45 done('should throw an error');46 } catch (error) {47 done();48 }49 });50 });5152 it('should throw when invoked with only one argument', (done) => {53 try {54 glue.contexts.subscribe('contextName');55 done('should throw an error');56 } catch (error) {57 done();58 }59 });6061 [undefined, null, false, true, '', 42, [], { tick: 42 }].forEach((invalidSecondArg) => {62 it(`should throw when invoked with an invalid second argument - ${JSON.stringify(invalidSecondArg)}`, (done) => {63 try {64 glue.contexts.subscribe('contextName', invalidSecondArg);65 done('should throw an error');66 } catch (error) {67 done();68 }69 });70 });7172 it('should invoke the callback when the specified context has been created with update()', (done) => {73 const contextName = gtf.contexts.getContextName();74 const context = { id: gtf.contexts.getContextName() };7576 glue.contexts77 .subscribe(contextName, (data) => {78 if (data.id === context.id) {79 done();80 }81 })82 .then((unsubscribeFn) => {83 gtf.addWindowHook(unsubscribeFn);84 return glue.contexts.update(contextName, context);85 })86 .catch((err) => done(err));87 });8889 it('should invoke the callback when the specified context has been created with set()', (done) => {90 const contextName = gtf.contexts.getContextName();91 const context = { id: gtf.contexts.getContextName() };9293 glue.contexts94 .subscribe(contextName, (data) => {95 if (data.id === context.id) {96 done();97 }98 })99 .then((unsubscribeFn) => {100 gtf.addWindowHook(unsubscribeFn);101 return glue.contexts.set(contextName, context);102 })103 .catch((err) => done(err));104 });105106 it('should invoke the callback when the specified context has been created with setPath()', (done) => {107 const contextName = gtf.contexts.getContextName();108109 glue.contexts110 .subscribe(contextName, (data) => {111 if (data.prop === 'value') {112 done();113 }114 })115 .then((unsubscribeFn) => {116 gtf.addWindowHook(unsubscribeFn);117 return glue.contexts.setPath(contextName, 'prop', 'value');118 })119 .catch((err) => done(err));120 });121122 it('should invoke the callback when the specified context has been created with setPaths()', (done) => {123 const contextName = gtf.contexts.getContextName();124 const contextPaths = [125 { path: 'prop1', value: 'value1' },126 { path: 'prop2', value: 'value2' },127 ];128129 glue.contexts130 .subscribe(contextName, (data) => {131 if (data.prop1 === 'value1') {132 done();133 }134 })135 .then((unsubscribeFn) => {136 gtf.addWindowHook(unsubscribeFn);137 return glue.contexts.setPaths(contextName, contextPaths);138 })139 .catch((err) => done(err));140 });141142 it('should invoke the callback with an object as first argument when the specified context has been created with update()', (done) => {143 const contextName = gtf.contexts.getContextName();144 const context = {145 id: gtf.contexts.getContextName(),146 complexObj: gtf.contexts.generateComplexObject(10),147 };148149 glue.contexts150 .subscribe(contextName, (updatedObject) => {151 if (updatedObject.id === context.id) {152 try {153 expect(updatedObject).to.be.an('object');154 done();155 } catch (error) {156 done(error);157 }158 }159 })160 .then((unsubscribeFn) => {161 gtf.addWindowHook(unsubscribeFn);162163 return glue.contexts.update(contextName, context);164 })165 .catch((err) => done(err));166 });167168 it('should invoke the callback with an object as first argument when the specified context has been created with set()', (done) => {169 const contextName = gtf.contexts.getContextName();170 const context = {171 id: gtf.contexts.getContextName(),172 complexObj: gtf.contexts.generateComplexObject(10),173 };174175 glue.contexts176 .subscribe(contextName, (updatedObject) => {177 if (updatedObject.id === context.id) {178 try {179 expect(updatedObject).to.be.an('object');180 done();181 } catch (error) {182 done(error);183 }184 }185 })186 .then((unsubscribeFn) => {187 gtf.addWindowHook(unsubscribeFn);188 return glue.contexts.set(contextName, context);189 })190 .catch((err) => done(err));191 });192193 it('should invoke the callback with an object as first argument when the specified context has been created with setPath()', (done) => {194 const contextName = gtf.contexts.getContextName();195 const context = {196 id: gtf.contexts.getContextName(),197 complexObj: gtf.contexts.generateComplexObject(10),198 };199200 glue.contexts201 .subscribe(contextName, (updatedObject) => {202 if (updatedObject.prop1.id === context.id) {203 try {204 expect(updatedObject).to.be.an('object');205 done();206 } catch (error) {207 done(error);208 }209 }210 })211 .then((unsubscribeFn) => {212 gtf.addWindowHook(unsubscribeFn);213 return glue.contexts.setPath(contextName, 'prop1', context);214 })215 .catch((err) => done(err));216 });217218 it('should invoke the callback with an object as first argument when the specified context has been created with setPaths()', (done) => {219 const contextName = gtf.contexts.getContextName();220221 glue.contexts222 .subscribe(contextName, (updatedObject) => {223 if (updatedObject.prop1 === 'value1') {224 try {225 expect(updatedObject).to.be.an('object');226 done();227 } catch (error) {228 done(error);229 }230 }231 })232 .then((unsubscribeFn) => {233 gtf.addWindowHook(unsubscribeFn);234 return glue.contexts.setPaths(contextName, [235 { path: 'prop1', value: 'value1' },236 { path: 'prop2', value: 'value2' },237 ]);238 })239240 .catch((err) => done(err));241 });242243 it('should invoke the callback with correct data as first argument when the specified context has been created with update()', (done) => {244 const contextName = gtf.contexts.getContextName();245 const context = {246 id: gtf.contexts.getContextName(),247 complexObj: gtf.contexts.generateComplexObject(10),248 };249250 glue.contexts251 .subscribe(contextName, (updatedObject) => {252 if (updatedObject.id === context.id) {253 try {254 expect(updatedObject).to.eql(context);255 done();256 } catch (error) {257 done(error);258 }259 }260 })261 .then((unsubscribeFn) => {262 gtf.addWindowHook(unsubscribeFn);263 return glue.contexts.update(contextName, context);264 })265 .catch((err) => done(err));266 });267268 it('should invoke the callback with correct data as first argument when the specified context has been created with set()', (done) => {269 const contextName = gtf.contexts.getContextName();270 const context = {271 id: gtf.contexts.getContextName(),272 complexObj: gtf.contexts.generateComplexObject(10),273 };274275 glue.contexts276 .subscribe(contextName, (updatedObject) => {277 if (updatedObject.id === context.id) {278 try {279 expect(updatedObject).to.eql(context);280 done();281 } catch (error) {282 done(error);283 }284 }285 })286 .then((unsubscribeFn) => {287 gtf.addWindowHook(unsubscribeFn);288 return glue.contexts.set(contextName, context);289 })290 .catch((err) => done(err));291 });292293 it('should invoke the callback with correct data as first argument when the specified context has been created with setPath()', (done) => {294 const contextName = gtf.contexts.getContextName();295 const context = {296 id: gtf.contexts.getContextName(),297 complexObj: gtf.contexts.generateComplexObject(10),298 };299300 glue.contexts301 .subscribe(contextName, (updatedObject) => {302 if (updatedObject.prop1.id === context.id) {303 try {304 expect(updatedObject).to.eql({ prop1: context });305 done();306 } catch (error) {307 done(error);308 }309 }310 })311 .then((unsubscribeFn) => {312 gtf.addWindowHook(unsubscribeFn);313 return glue.contexts.setPath(contextName, 'prop1', context);314 })315 .catch((err) => done(err));316 });317318 it('should invoke the callback with correct data as first argument when the specified context has been created with setPaths()', (done) => {319 const contextName = gtf.contexts.getContextName();320 const prop1Context = {321 id: gtf.contexts.getContextName(),322 complexObj: gtf.contexts.generateComplexObject(10),323 };324 const prop2Context = {325 title: 'prop2 context',326 value: 'prop2 value',327 };328329 glue.contexts330 .subscribe(contextName, (updatedObject) => {331 if (updatedObject.prop1.id === prop1Context.id) {332 try {333 expect(updatedObject).to.eql({334 prop1: prop1Context,335 prop2: prop2Context,336 });337 done();338 } catch (error) {339 done(error);340 }341 }342 })343 .then((unsubscribeFn) => {344 gtf.addWindowHook(unsubscribeFn);345 return glue.contexts.setPaths(contextName, [346 { path: 'prop1', value: prop1Context },347 { path: 'prop2', value: prop2Context },348 ]);349 })350351 .catch((err) => done(err));352 });353354 it('should invoke the callback 3 times when a context is updated 3 times with update()', (done) => {355 const ready = gtf.waitFor(3, done);356 const context = {357 id: gtf.contexts.getContextName(),358 name: gtf.contexts.getContextName(),359 complexObj: gtf.contexts.generateComplexObject(10),360 };361362 glue.contexts363 .subscribe(context.name, (data) => {364 if (data.id === context.id) {365 ready();366 }367 })368 .then((unsubscribeFn) => {369 gtf.addWindowHook(unsubscribeFn);370 return glue.contexts.update(context.name, context);371 })372 .then(() => glue.contexts.update(context.name, { date: new Date() }))373 .then(() => glue.contexts.update(context.name, { prop: 'value' }))374 .catch((err) => done(err));375 });376377 it('should invoke the callback 3 times when a context is changed 3 times with set()', (done) => {378 const ready = gtf.waitFor(3, done);379 const contextName = gtf.contexts.getContextName();380 const id = gtf.contexts.getContextName();381 const context = {382 id,383 name: gtf.contexts.getContextName(),384 complexObj: gtf.contexts.generateComplexObject(10),385 };386387 glue.contexts388 .subscribe(contextName, (data) => {389 if (data.id === context.id) {390 ready();391 }392 })393 .then((unsubscribeFn) => {394 gtf.addWindowHook(unsubscribeFn);395396 return glue.contexts.set(contextName, context);397 })398 .then(() => glue.contexts.set(contextName, { id, date: new Date() }))399 .then(() => glue.contexts.set(contextName, { id, prop: 'value' }))400 .catch((err) => done(err));401 });402403 it('should invoke the callback 3 times when a context is changed 3 times with setPath()', (done) => {404 const ready = gtf.waitFor(3, done);405 const context = {406 id: gtf.contexts.getContextName(),407 name: gtf.contexts.getContextName(),408 complexObj: gtf.contexts.generateComplexObject(10),409 };410411 glue.contexts412 .subscribe(context.name, (data) => {413 if (data.prop1.id === context.id) {414 ready();415 }416 })417 .then((unsubscribeFn) => {418 gtf.addWindowHook(unsubscribeFn);419 return glue.contexts.setPath(context.name, 'prop1', context);420 })421 .then(() => glue.contexts.setPath(context.name, 'prop2', { date: new Date() }))422 .then(() => glue.contexts.setPath(context.name, 'prop3', { prop: 'value' }))423 .catch((err) => done(err));424 });425426 it('should invoke the callback 3 times when a context is changed 3 times with setPaths()', (done) => {427 const ready = gtf.waitFor(3, done);428 const context = {429 id: gtf.contexts.getContextName(),430 name: gtf.contexts.getContextName(),431 complexObj: gtf.contexts.generateComplexObject(10),432 };433434 glue.contexts435 .subscribe(context.name, (data) => {436 if (data.prop1.id === context.id) {437 ready();438 }439 })440 .then((unsubscribeFn) => {441 gtf.addWindowHook(unsubscribeFn);442 return glue.contexts.setPaths(context.name, [{ path: 'prop1', value: context }]);443 })444 .then(() => glue.contexts.setPaths(context.name, [{ path: 'prop2', value: { date: new Date() } }]))445 .then(() => glue.contexts.setPaths(context.name, [{ path: 'prop3', value: { prop: 'value' } }]))446 .catch((err) => done(err));447 });448449 it('should invoke the callback with the updated data as first argument when the specified context has been changed with update()', (done) => {450 const context = {451 name: gtf.contexts.getContextName(),452 complexObj: gtf.contexts.generateComplexObject(10),453 };454 const newContext = {455 id: gtf.contexts.getContextName(),456 date: new Date(),457 title: 'new context',458 };459460 glue.contexts461 .update(context.name, context)462 .then(() =>463 glue.contexts.subscribe(context.name, (updatedObject) => {464 if (updatedObject.id === newContext.id) {465 try {466 expect(updatedObject).to.eql(Object.assign({}, context, newContext));467 done();468 } catch (error) {469 done(error);470 }471 }472 })473 )474 .then((unsubscribeFn) => {475 gtf.addWindowHook(unsubscribeFn);476 return glue.contexts.update(context.name, newContext);477 })478 .catch((err) => done(err));479 });480481 it('should invoke the callback with the new data as first argument when the specified context has been changed with set()', (done) => {482 const contextName = gtf.contexts.getContextName();483 const context = gtf.contexts.generateComplexObject(10);484 const newContext = {485 id: gtf.contexts.getContextName(),486 date: new Date(),487 title: 'new context',488 };489490 glue.contexts491 .set(contextName, context)492 .then(() =>493 glue.contexts.subscribe(contextName, (updatedObject) => {494 if (updatedObject.id === newContext.id) {495 try {496 expect(updatedObject).to.eql(newContext);497 done();498 } catch (error) {499 done(error);500 }501 }502 })503 )504 .then((unsubscribeFn) => {505 gtf.addWindowHook(unsubscribeFn);506 return glue.contexts.set(contextName, newContext);507 })508 .catch((err) => done(err));509 });510511 it('should invoke the callback with the new data as first argument when the specified context has been updated with setPath()', (done) => {512 const contextName = gtf.contexts.getContextName();513 const prop1Context = {514 date: new Date(),515 title: 'new context',516 };517 const prop2Context = {518 id: gtf.contexts.getContextName(),519 complexObj: gtf.contexts.generateComplexObject(10),520 };521522 glue.contexts523 .setPath(contextName, 'prop1', prop1Context)524 .then(() =>525 glue.contexts.subscribe(contextName, (updatedObject) => {526 if (updatedObject.prop2.id === prop2Context.id) {527 try {528 expect(updatedObject).to.eql({529 prop1: prop1Context,530 prop2: prop2Context,531 });532 done();533 } catch (error) {534 done(error);535 }536 }537 })538 )539 .then((unsubscribeFn) => {540 gtf.addWindowHook(unsubscribeFn);541 return glue.contexts.setPath(contextName, 'prop2', prop2Context);542 })543 .catch((err) => done(err));544 });545546 it('should invoke the callback with the new data as first argument when the specified context has been updated with setPaths()', (done) => {547 const contextName = gtf.contexts.getContextName();548 const prop1Context = {549 date: new Date(),550 title: 'new context',551 };552 const prop2Context = {553 id: gtf.contexts.getContextName(),554 complexObj: gtf.contexts.generateComplexObject(10),555 };556557 glue.contexts558 .setPaths(contextName, [{ path: 'prop1', value: prop1Context }])559 .then(() =>560 glue.contexts.subscribe(contextName, (updatedObject) => {561 if (updatedObject.prop2.id === prop2Context.id) {562 try {563 expect(updatedObject).to.eql({564 prop1: prop1Context,565 prop2: prop2Context,566 });567 done();568 } catch (error) {569 done(error);570 }571 }572 })573 )574 .then((unsubscribeFn) => {575 gtf.addWindowHook(unsubscribeFn);576 return glue.contexts.setPaths(contextName, [{ path: 'prop2', value: prop2Context }]);577 })578 .catch((err) => done(err));579 });580581 it('should invoke the callback with the object to update the context with as a second argument when the specified context has been updated with update()', (done) => {582 const contextName = gtf.contexts.getContextName();583 const initContext = {584 id: gtf.contexts.getContextName(),585 complexObj: gtf.contexts.generateComplexObject(10),586 };587 const newContext = gtf.contexts.generateComplexObject(10);588589 glue.contexts590 .update(contextName, initContext)591 .then(() =>592 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith) => {593 if (Object.keys(objectToUpdateWith).length) {594 try {595 expect(objectToUpdateWith).to.eql(newContext);596 done();597 } catch (error) {598 done(error);599 }600 }601 })602 )603 .then((unsubscribeFn) => {604 gtf.addWindowHook(unsubscribeFn);605 return glue.contexts.update(contextName, newContext);606 })607 .catch((err) => done(err));608 });609610 it('should invoke the callback with the object to update the context with as a second argument when the specified context has been changed with set()', (done) => {611 const contextName = gtf.contexts.getContextName();612 const initContext = gtf.contexts.generateComplexObject(10);613 const newContext = {614 id: gtf.contexts.getContextName(),615 complexObj: gtf.contexts.generateComplexObject(10),616 };617618 glue.contexts619 .set(contextName, initContext)620 .then(() =>621 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith) => {622 if (Object.keys(objectToUpdateWith).length) {623 try {624 expect(objectToUpdateWith).to.eql(newContext);625 done();626 } catch (error) {627 done(error);628 }629 }630 })631 )632 .then((unsubscribeFn) => {633 gtf.addWindowHook(unsubscribeFn);634 return glue.contexts.set(contextName, newContext);635 })636 .catch((err) => done(err));637 });638639 it('should invoke the callback with the object to update the context with as a second argument when the specified context has been updated with setPath()', (done) => {640 const contextName = gtf.contexts.getContextName();641 const prop1Context = {642 id: gtf.contexts.getContextName(),643 complexObj: gtf.contexts.generateComplexObject(10),644 };645 const prop2Context = {646 date: new Date(),647 title: 'new context',648 };649650 glue.contexts651 .setPath(contextName, 'prop1', prop1Context)652 .then(() =>653 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith) => {654 if (Object.keys(objectToUpdateWith).length) {655 try {656 expect(objectToUpdateWith).to.eql({ prop2: prop2Context });657 done();658 } catch (error) {659 done(error);660 }661 }662 })663 )664 .then((unsubscribeFn) => {665 gtf.addWindowHook(unsubscribeFn);666 return glue.contexts.setPath(contextName, 'prop2', prop2Context);667 })668 .catch((err) => done(err));669 });670671 it('should invoke the callback with the object to update the context with as a second argument when the specified context has been updated with setPaths()', (done) => {672 const contextName = gtf.contexts.getContextName();673 const prop1Context = {674 date: new Date(),675 title: 'new context',676 };677 const prop2Context = {678 id: gtf.contexts.getContextName(),679 complexObj: gtf.contexts.generateComplexObject(10),680 };681682 glue.contexts683 .setPaths(contextName, [{ path: 'prop1', value: prop1Context }])684 .then(() =>685 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith) => {686 if (Object.keys(objectToUpdateWith).length) {687 try {688 expect(objectToUpdateWith).to.eql({689 prop2: prop2Context,690 });691 done();692 } catch (error) {693 done(error);694 }695 }696 })697 )698 .then((unsubscribeFn) => {699 gtf.addWindowHook(unsubscribeFn);700 return glue.contexts.setPaths(contextName, [{ path: 'prop2', value: prop2Context }]);701 })702 .catch((err) => done(err));703 });704705 it('should invoke the callback with an array as third argument when context is updated with existing keys set to null with update()', (done) => {706 const contextName = gtf.contexts.getContextName();707 const context = {708 name: gtf.contexts.getContextName(),709 complexObj: gtf.contexts.generateComplexObject(10),710 id: 'unique identifier',711 title: 'title',712 date: new Date(),713 };714715 glue.contexts716 .update(contextName, context)717 .then(() =>718 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith, removedKeys) => {719 if (Object.keys(objectToUpdateWith).length) {720 try {721 expect(removedKeys).to.eql(['title', 'date']);722 done();723 } catch (error) {724 done(error);725 }726 }727 })728 )729 .then((unsubscribeFn) => {730 gtf.addWindowHook(unsubscribeFn);731 return glue.contexts.update(contextName, { title: null, date: null });732 })733 .catch((err) => done(err));734 });735736 it('should invoke the callback with an array of the removed keys as third argument when context is updated with existing keys set to null with setPath()', (done) => {737 const contextName = gtf.contexts.getContextName();738739 glue.contexts740 .setPath(contextName, 'prop1.prop2', 'prop2 value')741 .then(() =>742 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith, removedKeys) => {743 if (Object.keys(objectToUpdateWith).length) {744 try {745 expect(removedKeys).to.be.an('array');746 expect(removedKeys).to.eql(['prop1']);747 done();748 } catch (error) {749 done(error);750 }751 }752 })753 )754 .then((unsubscribeFn) => {755 gtf.addWindowHook(unsubscribeFn);756 return glue.contexts.setPath(contextName, 'prop1', null);757 })758 .catch((err) => done(err));759 });760761 it('should invoke the callback with an array of the removed keys as third argument when context is updated with existing keys set to null with setPaths()', (done) => {762 const contextName = gtf.contexts.getContextName();763764 glue.contexts765 .setPaths(contextName, [{ path: 'prop1.prop2', value: 'prop2 value' }])766 .then(() =>767 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith, removedKeys) => {768 if (Object.keys(objectToUpdateWith).length) {769 try {770 expect(removedKeys).to.be.an('array');771 expect(removedKeys).to.eql(['prop1']);772 done();773 } catch (error) {774 done(error);775 }776 }777 })778 )779 .then((unsubscribeFn) => {780 gtf.addWindowHook(unsubscribeFn);781 return glue.contexts.setPaths(contextName, [{ path: 'prop1', value: null }]);782 })783 .catch((err) => done(err));784 });785786 it('should invoke the callback with an unsubscribe function as a fourth argument and be able to unsubscribe when invoking that function', (done) => {787 const contextName = gtf.contexts.getContextName();788 const context = {789 id: gtf.contexts.getContextName(),790 complexObj: gtf.contexts.generateComplexObject(10),791 date: new Date(),792 };793794 let counter = 0;795796 glue.contexts797 .subscribe(contextName, (updatedObject, objectToUpdateWith, removedKeys, unsubscribeFn) => {798 if (updatedObject.id === context.id) {799 if (updatedObject.unsubscribe === true) {800 unsubscribeFn();801 } else {802 counter++;803 }804 }805 })806 .then((unsubscribeFn) => {807 gtf.addWindowHook(unsubscribeFn);808 return glue.contexts.update(contextName, context);809 })810 .then(() => glue.contexts.update(contextName, { test: 'test' }))811 .then(() => glue.contexts.update(contextName, { tick: 42 }))812 .then(() => glue.contexts.update(contextName, { unsubscribe: true }))813 .then(() => glue.contexts.update(contextName, { glue: 42 }))814 .then(() => glue.contexts.update(contextName, { prop: 'value' }))815 .then(() => {816 expect(counter).to.eql(3);817 done();818 })819 .catch((err) => done(err));820 });821822 it('should return a function', (done) => {823 const contextName = gtf.contexts.getContextName();824825 glue.contexts826 .subscribe(contextName, () => {})827 .then((unsubscribeFn) => {828 gtf.addWindowHook(unsubscribeFn);829 expect(unsubscribeFn).to.be.a('function');830 done();831 })832 .catch((err) => done(err));833 });834835 it('should return an unsubscribe function which unsubscribes from context updates when invoked', (done) => {836 const contextName = gtf.contexts.getContextName();837 const context = {838 id: gtf.contexts.getContextName(),839 complexObj: gtf.contexts.generateComplexObject(10),840 };841842 let un;843 let counter = 0;844845 glue.contexts846 .subscribe(contextName, (data) => {847 if (data.id === context.id) {848 counter++;849 }850 })851 .then((unsubscribeFn) => {852 un = unsubscribeFn;853 gtf.addWindowHook(un);854 return glue.contexts.update(contextName, context);855 })856 .then(() => glue.contexts.update(contextName, { tick: 42 }))857 .then(() => glue.contexts.update(contextName, { glue: 42 }))858 .then(() => un())859 .then(() => glue.contexts.update(contextName, { data: 'new data' }))860 .then(() => glue.contexts.update(contextName, { date: new Date() }))861 .then(() => {862 expect(counter).to.eql(3);863 done();864 })865 .catch((err) => done(err));866 });867868 it('should return snapshot when get() is followed by subscribe()', (done) => {869 const contextName = gtf.contexts.getContextName();870 const context = gtf.contexts.generateComplexObject(10);871 const ready = gtf.waitFor(2, done);872873 glue.contexts874 .subscribe(contextName, async () => {875 try {876 const contextData = await glue.contexts.get(contextName);877 expect(contextData).to.eql(context);878 ready();879 } catch (error) {880 done(error);881 }882883 await glue.contexts.subscribe(contextName, (dataFromSubscribe) => {884 try {885 expect(dataFromSubscribe).to.eql(context);886 ready();887 } catch (error) {888 done(error);889 }890 });891 })892 .then((unFn) => {893 gtf.addWindowHook(unFn);894 return glue.contexts.update(contextName, context);895 })896 .catch((err) => done(err));897 });898 });899900 describe('when manipulated by another app, ', function () {901 let secondApp;902903 beforeEach(async () => {904 secondApp = await gtf.createApp();905 });906907 afterEach(() => secondApp.stop());908909 it('should be invoked when a context is updated and is invoked after the context was created', (done) => {910 const contextName = gtf.contexts.getContextName();911 const sampleContext = { test: 1 };912913 secondApp.contexts914 .update(contextName, sampleContext)915 .then(() => {916 return glue.contexts.subscribe(contextName, () => {917 done();918 });919 })920 .then((unsubscribeFn) => gtf.addWindowHook(unsubscribeFn))921 .then(() => {922 return secondApp.contexts.update(contextName, { test: 2 });923 })924 .catch(done);925 });926927 it('should be invoked when a context is set and is invoked after the context was created', (done) => {928 const contextName = gtf.contexts.getContextName();929 const sampleContext = { test: 1 };930931 secondApp.contexts932 .set(contextName, sampleContext)933 .then(() => {934 return glue.contexts.subscribe(contextName, () => {935 done();936 });937 })938 .then((unsubscribeFn) => gtf.addWindowHook(unsubscribeFn))939 .then(() => {940 return secondApp.contexts.set(contextName, { test: 2 });941 })942 .catch(done);943 });944945 it('should invoke the callback when the specified context has been created with update()', (done) => {946 const contextName = gtf.contexts.getContextName();947 const context = { id: gtf.contexts.getContextName() };948949 glue.contexts950 .subscribe(contextName, (data) => {951 if (data.id === context.id) {952 done();953 }954 })955 .then((unsubscribeFn) => {956 gtf.addWindowHook(unsubscribeFn);957 return secondApp.contexts.update(contextName, context);958 })959 .catch((err) => done(err));960 });961962 it('should invoke the callback when the specified context has been created with set()', (done) => {963 const contextName = gtf.contexts.getContextName();964 const context = { id: gtf.contexts.getContextName() };965966 glue.contexts967 .subscribe(contextName, (data) => {968 if (data.id === context.id) {969 done();970 }971 })972 .then((unsubscribeFn) => {973 gtf.addWindowHook(unsubscribeFn);974 return secondApp.contexts.set(contextName, context);975 })976 .catch((err) => done(err));977 });978979 it('should invoke the callback when the specified context has been created with setPath()', (done) => {980 const contextName = gtf.contexts.getContextName();981982 glue.contexts983 .subscribe(contextName, (data) => {984 if (data.prop === 'value') {985 done();986 }987 })988 .then((unsubscribeFn) => {989 gtf.addWindowHook(unsubscribeFn);990 return secondApp.contexts.setPath(contextName, 'prop', 'value');991 })992 .catch((err) => done(err));993 });994995 it('should invoke the callback when the specified context has been created with setPaths()', (done) => {996 const contextName = gtf.contexts.getContextName();997 const contextPaths = [998 { path: 'prop1', value: 'value1' },999 { path: 'prop2', value: 'value2' },1000 ];10011002 glue.contexts1003 .subscribe(contextName, (data) => {1004 if (data.prop1 === 'value1') {1005 done();1006 }1007 })1008 .then((unsubscribeFn) => {1009 gtf.addWindowHook(unsubscribeFn);1010 return secondApp.contexts.setPaths(contextName, contextPaths);1011 })1012 .catch((err) => done(err));1013 });10141015 it('should invoke the callback with an object as first argument when the specified context has been created with update()', (done) => {1016 const contextName = gtf.contexts.getContextName();1017 const context = {1018 id: gtf.contexts.getContextName(),1019 complexObj: gtf.contexts.generateComplexObject(10),1020 };10211022 glue.contexts1023 .subscribe(contextName, (updatedObject) => {1024 try {1025 expect(updatedObject).to.be.an('object');1026 done();1027 } catch (error) {1028 done(error);1029 }1030 })1031 .then((unsubscribeFn) => {1032 gtf.addWindowHook(unsubscribeFn);1033 return secondApp.contexts.update(contextName, context);1034 })1035 .catch((err) => done(err));1036 });10371038 it('should invoke the callback with an object as first argument when the specified context has been created with set()', (done) => {1039 const contextName = gtf.contexts.getContextName();1040 const context = {1041 id: gtf.contexts.getContextName(),1042 complexObj: gtf.contexts.generateComplexObject(10),1043 };10441045 glue.contexts1046 .subscribe(contextName, (updatedObject) => {1047 if (updatedObject.id === context.id) {1048 try {1049 expect(updatedObject).to.be.an('object');1050 done();1051 } catch (error) {1052 done(error);1053 }1054 }1055 })1056 .then((unsubscribeFn) => {1057 gtf.addWindowHook(unsubscribeFn);1058 return secondApp.contexts.set(contextName, context);1059 })1060 .catch((err) => done(err));1061 });10621063 it('should invoke the callback with an object as first argument when the specified context has been created with setPath()', (done) => {1064 const contextName = gtf.contexts.getContextName();1065 const context = {1066 id: gtf.contexts.getContextName(),1067 complexObj: gtf.contexts.generateComplexObject(10),1068 };10691070 glue.contexts1071 .subscribe(contextName, (updatedObject) => {1072 if (updatedObject.prop1.id === context.id) {1073 try {1074 expect(updatedObject).to.be.an('object');1075 done();1076 } catch (error) {1077 done(error);1078 }1079 }1080 })1081 .then((unsubscribeFn) => {1082 gtf.addWindowHook(unsubscribeFn);1083 return secondApp.contexts.setPath(contextName, 'prop1', context);1084 })1085 .catch((err) => done(err));1086 });10871088 it('should invoke the callback with an object as first argument when the specified context has been created with setPaths()', (done) => {1089 const contextName = gtf.contexts.getContextName();10901091 glue.contexts1092 .subscribe(contextName, (updatedObject) => {1093 if (updatedObject.prop1 === 'value1') {1094 try {1095 expect(updatedObject).to.be.an('object');1096 done();1097 } catch (error) {1098 done(error);1099 }1100 }1101 })1102 .then((unsubscribeFn) => {1103 gtf.addWindowHook(unsubscribeFn);1104 return secondApp.contexts.setPaths(contextName, [1105 { path: 'prop1', value: 'value1' },1106 { path: 'prop2', value: 'value2' },1107 ]);1108 })11091110 .catch((err) => done(err));1111 });11121113 it('should invoke the callback with correct data as first argument when the specified context has been created with update()', (done) => {1114 const contextName = gtf.contexts.getContextName();1115 const context = {1116 id: gtf.contexts.getContextName(),1117 complexObj: gtf.contexts.generateComplexObject(10),1118 };11191120 glue.contexts1121 .subscribe(contextName, (updatedObject) => {1122 if (updatedObject.id === context.id) {1123 try {1124 expect(updatedObject).to.eql(context);1125 done();1126 } catch (error) {1127 done(error);1128 }1129 }1130 })1131 .then((unsubscribeFn) => {1132 gtf.addWindowHook(unsubscribeFn);1133 return secondApp.contexts.update(contextName, context);1134 })1135 .catch((err) => done(err));1136 });11371138 it('should invoke the callback with correct data as first argument when the specified context has been created with set()', (done) => {1139 const contextName = gtf.contexts.getContextName();1140 const context = {1141 id: gtf.contexts.getContextName(),1142 complexObj: gtf.contexts.generateComplexObject(10),1143 };11441145 glue.contexts1146 .subscribe(contextName, (updatedObject) => {1147 if (updatedObject.id === context.id) {1148 try {1149 expect(updatedObject).to.eql(context);1150 done();1151 } catch (error) {1152 done(error);1153 }1154 }1155 })1156 .then((unsubscribeFn) => {1157 gtf.addWindowHook(unsubscribeFn);1158 return secondApp.contexts.set(contextName, context);1159 })1160 .catch((err) => done(err));1161 });11621163 it('should invoke the callback with correct data as first argument when the specified context has been created with setPath()', (done) => {1164 const contextName = gtf.contexts.getContextName();1165 const context = {1166 id: gtf.contexts.getContextName(),1167 complexObj: gtf.contexts.generateComplexObject(10),1168 };11691170 glue.contexts1171 .subscribe(contextName, (updatedObject) => {1172 if (updatedObject.prop1.id === context.id) {1173 try {1174 expect(updatedObject).to.eql({ prop1: context });1175 done();1176 } catch (error) {1177 done(error);1178 }1179 }1180 })1181 .then((unsubscribeFn) => {1182 gtf.addWindowHook(unsubscribeFn);1183 return secondApp.contexts.setPath(contextName, 'prop1', context);1184 })1185 .catch((err) => done(err));1186 });11871188 it('should invoke the callback with correct data as first argument when the specified context has been created with setPaths()', (done) => {1189 const contextName = gtf.contexts.getContextName();1190 const prop1Context = {1191 id: gtf.contexts.getContextName(),1192 complexObj: gtf.contexts.generateComplexObject(10),1193 };1194 const prop2Context = {1195 title: 'prop2 context',1196 value: 'prop2 value',1197 };11981199 glue.contexts1200 .subscribe(contextName, (updatedObject) => {1201 if (updatedObject.prop1.id === prop1Context.id) {1202 try {1203 expect(updatedObject).to.eql({1204 prop1: prop1Context,1205 prop2: prop2Context,1206 });1207 done();1208 } catch (error) {1209 done(error);1210 }1211 }1212 })1213 .then((unsubscribeFn) => {1214 gtf.addWindowHook(unsubscribeFn);1215 return secondApp.contexts.setPaths(contextName, [1216 { path: 'prop1', value: prop1Context },1217 { path: 'prop2', value: prop2Context },1218 ]);1219 })12201221 .catch((err) => done(err));1222 });12231224 it('should invoke the callback 3 times when a context is updated 3 times with update()', (done) => {1225 const ready = gtf.waitFor(3, done);1226 const context = {1227 id: gtf.contexts.getContextName(),1228 name: gtf.contexts.getContextName(),1229 complexObj: gtf.contexts.generateComplexObject(10),1230 };12311232 glue.contexts1233 .subscribe(context.name, (data) => {1234 if (data.id === context.id) {1235 ready();1236 }1237 })1238 .then((unsubscribeFn) => {1239 gtf.addWindowHook(unsubscribeFn);1240 return secondApp.contexts.update(context.name, context);1241 })1242 .then(() => secondApp.contexts.update(context.name, { date: new Date() }))1243 .then(() => secondApp.contexts.update(context.name, { prop: 'value' }))1244 .catch((err) => done(err));1245 });12461247 it('should invoke the callback 3 times when a context is changed 3 times with set()', (done) => {1248 const ready = gtf.waitFor(3, done);1249 const contextName = gtf.contexts.getContextName();1250 const id = gtf.contexts.getContextName();12511252 const context = {1253 id,1254 name: gtf.contexts.getContextName(),1255 complexObj: gtf.contexts.generateComplexObject(10),1256 };12571258 glue.contexts1259 .subscribe(contextName, (data) => {1260 if (data.id === context.id) {1261 ready();1262 }1263 })1264 .then((unsubscribeFn) => {1265 gtf.addWindowHook(unsubscribeFn);1266 return secondApp.contexts.set(contextName, context);1267 })1268 .then(() => secondApp.contexts.set(contextName, { id, date: new Date() }))1269 .then(() => secondApp.contexts.set(contextName, { id, prop: 'value' }))1270 .catch((err) => done(err));1271 });12721273 it('should invoke the callback 3 times when a context is changed 3 times with setPath()', (done) => {1274 const ready = gtf.waitFor(3, done);1275 const context = {1276 id: gtf.contexts.getContextName(),1277 name: gtf.contexts.getContextName(),1278 complexObj: gtf.contexts.generateComplexObject(10),1279 };12801281 glue.contexts1282 .subscribe(context.name, (data) => {1283 if (data.prop1.id === context.id) {1284 ready();1285 }1286 })1287 .then((unsubscribeFn) => {1288 gtf.addWindowHook(unsubscribeFn);1289 return secondApp.contexts.setPath(context.name, 'prop1', context);1290 })1291 .then(() => secondApp.contexts.setPath(context.name, 'prop2', { date: new Date() }))1292 .then(() => secondApp.contexts.setPath(context.name, 'prop3', { prop: 'value' }))1293 .catch((err) => done(err));1294 });12951296 it('should invoke the callback 3 times when a context is changed 3 times with setPaths()', (done) => {1297 const ready = gtf.waitFor(3, done);1298 const context = {1299 id: gtf.contexts.getContextName(),1300 name: gtf.contexts.getContextName(),1301 complexObj: gtf.contexts.generateComplexObject(10),1302 };13031304 glue.contexts1305 .subscribe(context.name, (data) => {1306 if (data.prop1.id === context.id) {1307 ready();1308 }1309 })1310 .then((unsubscribeFn) => {1311 gtf.addWindowHook(unsubscribeFn);1312 return secondApp.contexts.setPaths(context.name, [{ path: 'prop1', value: context }]);1313 })1314 .then(() => secondApp.contexts.setPaths(context.name, [{ path: 'prop2', value: { date: new Date() } }]))1315 .then(() => secondApp.contexts.setPaths(context.name, [{ path: 'prop3', value: { prop: 'value' } }]))1316 .catch((err) => done(err));1317 });13181319 it('should invoke the callback with the updated data as first argument when the specified context has been changed with update()', (done) => {1320 const context = {1321 name: gtf.contexts.getContextName(),1322 complexObj: gtf.contexts.generateComplexObject(10),1323 };1324 const newContext = {1325 id: gtf.contexts.getContextName(),1326 date: new Date(),1327 title: 'new context',1328 };13291330 glue.contexts1331 .update(context.name, context)1332 .then(() => {1333 return glue.contexts.subscribe(context.name, (updatedObject) => {1334 if (updatedObject.id === newContext.id) {1335 try {1336 expect(updatedObject).to.eql(Object.assign({}, context, newContext));1337 done();1338 } catch (error) {1339 done(error);1340 }1341 }1342 });1343 })1344 .then((unsubscribeFn) => {1345 gtf.addWindowHook(unsubscribeFn);1346 return secondApp.contexts.update(context.name, newContext);1347 })1348 .catch((err) => done(err));1349 });13501351 it('should invoke the callback with the new data as first argument when the specified context has been changed with set()', (done) => {1352 const contextName = gtf.contexts.getContextName();1353 const context = gtf.contexts.generateComplexObject(10);1354 const newContext = {1355 id: gtf.contexts.getContextName(),1356 date: new Date(),1357 title: 'new context',1358 };13591360 glue.contexts1361 .set(contextName, context)1362 .then(() => {1363 return glue.contexts.subscribe(contextName, (updatedObject) => {1364 if (updatedObject.id === newContext.id) {1365 try {1366 expect(updatedObject).to.eql(newContext);1367 done();1368 } catch (error) {1369 done(error);1370 }1371 }1372 });1373 })1374 .then((unsubscribeFn) => {1375 gtf.addWindowHook(unsubscribeFn);1376 return secondApp.contexts.set(contextName, newContext);1377 })1378 .catch((err) => done(err));1379 });13801381 it('should invoke the callback with the new data as first argument when the specified context has been updated with setPath()', (done) => {1382 const contextName = gtf.contexts.getContextName();1383 const prop1Context = {1384 date: new Date(),1385 title: 'new context',1386 };1387 const prop2Context = {1388 id: gtf.contexts.getContextName(),1389 complexObj: gtf.contexts.generateComplexObject(10),1390 };13911392 secondApp.contexts1393 .setPath(contextName, 'prop1', prop1Context)1394 .then(() =>1395 glue.contexts.subscribe(contextName, (updatedObject) => {1396 if (updatedObject.prop2.id === prop2Context.id) {1397 try {1398 expect(updatedObject).to.eql({1399 prop1: prop1Context,1400 prop2: prop2Context,1401 });1402 done();1403 } catch (error) {1404 done(error);1405 }1406 }1407 })1408 )1409 .then((unsubscribeFn) => {1410 gtf.addWindowHook(unsubscribeFn);1411 return secondApp.contexts.setPath(contextName, 'prop2', prop2Context);1412 })1413 .catch((err) => done(err));1414 });14151416 it('should invoke the callback with the new data as first argument when the specified context has been updated with setPaths()', (done) => {1417 const contextName = gtf.contexts.getContextName();1418 const prop1Context = {1419 date: new Date(),1420 title: 'new context',1421 };1422 const prop2Context = {1423 id: gtf.contexts.getContextName(),1424 complexObj: gtf.contexts.generateComplexObject(10),1425 };14261427 secondApp.contexts1428 .setPaths(contextName, [{ path: 'prop1', value: prop1Context }])1429 .then(() =>1430 glue.contexts.subscribe(contextName, (updatedObject) => {1431 if (updatedObject.prop2.id === prop2Context.id) {1432 try {1433 expect(updatedObject).to.eql({1434 prop1: prop1Context,1435 prop2: prop2Context,1436 });1437 done();1438 } catch (error) {1439 done(error);1440 }1441 }1442 })1443 )1444 .then((unsubscribeFn) => {1445 gtf.addWindowHook(unsubscribeFn);1446 return secondApp.contexts.setPaths(contextName, [{ path: 'prop2', value: prop2Context }]);1447 })1448 .catch((err) => done(err));1449 });14501451 it('should invoke the callback with the object to update the context with as a second argument when the specified context has been updated with update()', (done) => {1452 const contextName = gtf.contexts.getContextName();1453 const initContext = {1454 id: gtf.contexts.getContextName(),1455 complexObj: gtf.contexts.generateComplexObject(10),1456 };1457 const newContext = gtf.contexts.generateComplexObject(10);14581459 glue.contexts1460 .update(contextName, initContext)1461 .then(() => {1462 return glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith) => {1463 if (Object.keys(objectToUpdateWith).length) {1464 try {1465 expect(objectToUpdateWith).to.eql(newContext);1466 done();1467 } catch (error) {1468 done(error);1469 }1470 }1471 });1472 })1473 .then((unsubscribeFn) => {1474 gtf.addWindowHook(unsubscribeFn);1475 return secondApp.contexts.update(contextName, newContext);1476 })1477 .catch((err) => done(err));1478 });14791480 it('should invoke the callback with the object to update the context with as a second argument when the specified context has been changed with set()', (done) => {1481 const contextName = gtf.contexts.getContextName();1482 const initContext = gtf.contexts.generateComplexObject(10);1483 const newContext = {1484 id: gtf.contexts.getContextName(),1485 complexObj: gtf.contexts.generateComplexObject(10),1486 };14871488 glue.contexts1489 .set(contextName, initContext)1490 .then(() => {1491 return glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith) => {1492 if (Object.keys(objectToUpdateWith).length) {1493 try {1494 expect(objectToUpdateWith).to.eql(newContext);1495 done();1496 } catch (error) {1497 done(error);1498 }1499 }1500 });1501 })1502 .then((unsubscribeFn) => {1503 gtf.addWindowHook(unsubscribeFn);1504 return secondApp.contexts.set(contextName, newContext);1505 })1506 .catch((err) => done(err));1507 });15081509 it('should invoke the callback with the object to update the context with as a second argument when the specified context has been updated with setPath()', (done) => {1510 const contextName = gtf.contexts.getContextName();1511 const prop1Context = {1512 id: gtf.contexts.getContextName(),1513 complexObj: gtf.contexts.generateComplexObject(10),1514 };1515 const prop2Context = {1516 date: new Date(),1517 title: 'new context',1518 };15191520 secondApp.contexts1521 .setPath(contextName, 'prop1', prop1Context)1522 .then(() =>1523 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith) => {1524 if (Object.keys(objectToUpdateWith).length) {1525 try {1526 expect(objectToUpdateWith).to.eql({ prop2: prop2Context });1527 done();1528 } catch (error) {1529 done(error);1530 }1531 }1532 })1533 )1534 .then((unsubscribeFn) => {1535 gtf.addWindowHook(unsubscribeFn);1536 return secondApp.contexts.setPath(contextName, 'prop2', prop2Context);1537 })1538 .catch((err) => done(err));1539 });15401541 it('should invoke the callback with the object to update the context with as a second argument when the specified context has been updated with setPaths()', (done) => {1542 const contextName = gtf.contexts.getContextName();1543 const prop1Context = {1544 date: new Date(),1545 title: 'new context',1546 };1547 const prop2Context = {1548 id: gtf.contexts.getContextName(),1549 complexObj: gtf.contexts.generateComplexObject(10),1550 };15511552 secondApp.contexts1553 .setPaths(contextName, [{ path: 'prop1', value: prop1Context }])1554 .then(() =>1555 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith) => {1556 if (Object.keys(objectToUpdateWith).length) {1557 try {1558 expect(objectToUpdateWith).to.eql({1559 prop2: prop2Context,1560 });1561 done();1562 } catch (error) {1563 done(error);1564 }1565 }1566 })1567 )1568 .then((unsubscribeFn) => {1569 gtf.addWindowHook(unsubscribeFn);1570 return secondApp.contexts.setPaths(contextName, [{ path: 'prop2', value: prop2Context }]);1571 })1572 .catch((err) => done(err));1573 });15741575 it('should invoke the callback with an array as third argument when context is updated with existing keys set to null with update()', (done) => {1576 const contextName = gtf.contexts.getContextName();1577 const context = {1578 name: gtf.contexts.getContextName(),1579 complexObj: gtf.contexts.generateComplexObject(10),1580 id: 'unique identifier',1581 title: 'title',1582 date: new Date(),1583 };15841585 glue.contexts1586 .update(contextName, context)1587 .then(() => {1588 return glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith, removedKeys) => {1589 if (Object.keys(objectToUpdateWith).length) {1590 try {1591 expect(removedKeys).to.eql(['title', 'date']);1592 done();1593 } catch (error) {1594 done(error);1595 }1596 }1597 });1598 })1599 .then((unsubscribeFn) => {1600 gtf.addWindowHook(unsubscribeFn);1601 return secondApp.contexts.update(contextName, { title: null, date: null });1602 })1603 .catch((err) => done(err));1604 });16051606 it('should invoke the callback with an array of the removed keys as third argument when context is updated with existing keys set to null with setPath()', (done) => {1607 const contextName = gtf.contexts.getContextName();16081609 secondApp.contexts1610 .setPath(contextName, 'prop1.prop2', 'prop2 value')1611 .then(() =>1612 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith, removedKeys) => {1613 if (Object.keys(objectToUpdateWith).length) {1614 try {1615 expect(removedKeys).to.be.an('array');1616 expect(removedKeys).to.eql(['prop1']);1617 done();1618 } catch (error) {1619 done(error);1620 }1621 }1622 })1623 )1624 .then((unsubscribeFn) => {1625 gtf.addWindowHook(unsubscribeFn);1626 return secondApp.contexts.setPath(contextName, 'prop1', null);1627 })1628 .catch((err) => done(err));1629 });16301631 it('should invoke the callback with an array of the removed keys as third argument when context is updated with existing keys set to null with setPaths()', (done) => {1632 const contextName = gtf.contexts.getContextName();16331634 secondApp.contexts1635 .setPaths(contextName, [{ path: 'prop1.prop2', value: 'prop2 value' }])1636 .then(() =>1637 glue.contexts.subscribe(contextName, (updatedObject, objectToUpdateWith, removedKeys) => {1638 if (Object.keys(objectToUpdateWith).length) {1639 try {1640 expect(removedKeys).to.be.an('array');1641 expect(removedKeys).to.eql(['prop1']);1642 done();1643 } catch (error) {1644 done(error);1645 }1646 }1647 })1648 )1649 .then((unsubscribeFn) => {1650 gtf.addWindowHook(unsubscribeFn);1651 return secondApp.contexts.setPaths(contextName, [{ path: 'prop1', value: null }]);1652 })1653 .catch((err) => done(err));1654 });16551656 it('should invoke the callback with an unsubscribe function as a fourth argument and be able to unsubscribe when invoking that function', (done) => {1657 const contextName = gtf.contexts.getContextName();1658 const context = {1659 id: gtf.contexts.getContextName(),1660 complexObj: gtf.contexts.generateComplexObject(10),1661 date: new Date(),1662 };16631664 let counter = 0;16651666 glue.contexts1667 .subscribe(contextName, (_u, _o, _r, unsubscribeFn) => {1668 if (_u.id === context.id) {1669 if (_u.unsubscribe === true) {1670 unsubscribeFn();1671 } else {1672 counter++;1673 }1674 }1675 })1676 .then((unsubscribeFn) => {1677 gtf.addWindowHook(unsubscribeFn);1678 return secondApp.contexts.update(contextName, context);1679 })1680 .then(() => secondApp.contexts.update(contextName, { test: 'test' }))1681 .then(() => secondApp.contexts.update(contextName, { tick: 42 }))1682 .then(() => secondApp.contexts.update(contextName, { unsubscribe: true }))1683 .then(() => secondApp.contexts.update(contextName, { glue: 42 }))1684 .then(() => secondApp.contexts.update(contextName, { prop: 'value' }))1685 .then(() => {1686 expect(counter).to.eql(3);1687 done();1688 })1689 .catch((err) => done(err));1690 });16911692 it('should return an unsubscribe function which unsubscribes from context updates when invoked', async () => {1693 const context = {1694 name: gtf.contexts.getContextName(),1695 complexObj: gtf.contexts.generateComplexObject(10),1696 };1697 let counter = 0;16981699 const unsubscribeFn = await glue.contexts.subscribe(context.name, () => {1700 counter++;1701 });1702 gtf.addWindowHook(unsubscribeFn);17031704 await secondApp.contexts.update(context.name, {1705 anotherComplexObj: gtf.contexts.generateComplexObject(10),1706 });1707 await secondApp.contexts.update(context.name, { tick: 42 });1708 await secondApp.contexts.update(context.name, { glue: 42 });17091710 unsubscribeFn();17111712 await secondApp.contexts.update(context.name, { data: 'new data' });1713 await secondApp.contexts.update(context.name, { date: new Date() });17141715 expect(counter).to.eql(3);1716 });17171718 it("should return correct data when another app resolves creating a context and subscribe should also return correct data when there's a subscription for the given context", (done) => {1719 const contextName = gtf.contexts.getContextName();1720 const context = gtf.contexts.generateComplexObject(10);17211722 const ready = gtf.waitFor(2, done);17231724 secondApp.contexts1725 .update(contextName, context)1726 .then(() => {1727 return glue.contexts.subscribe(contextName, (data) => {1728 try {1729 expect(data).to.eql(context);1730 ready();1731 } catch (error) {1732 done(error);1733 }1734 });1735 })1736 .then((unsubscribeFn) => {1737 gtf.addWindowHook(unsubscribeFn);1738 return glue.contexts.get(contextName);1739 })1740 .then((ctxData) => {1741 expect(ctxData).to.eql(context);1742 ready();1743 })1744 .catch((err) => done(err));1745 });1746 }); ...

Full Screen

Full Screen

update.spec.js

Source:update.spec.js Github

copy

Full Screen

1describe('update() ', () => {2 const contextsForTesting = [3 { type: 'simple' },4 {5 type: 'simpleWithArray',6 arr: [1, 2, 3],7 },8 {9 type: 'arrayWithDifferentTypes',10 arr: [1, '2', {}],11 },12 {13 type: 'nestedObjects',14 first: {15 second: {16 third: {},17 },18 },19 },20 {21 type: 'cubeRepresentation',22 cube: [[[1]], [[2]], [[3]]],23 },24 ];2526 before(async () => {27 await coreReady;2829 initialContexts = await Promise.all(30 glue.contexts.all().map((contextName) => {31 const context = glue.contexts.get(contextName);32 return { name: contextName, context };33 })34 );3536 return Promise.all(glue.contexts.all().map((context) => glue.contexts.destroy(context)));37 });3839 afterEach(() => {40 gtf.clearWindowActiveHooks();4142 return Promise.all(glue.contexts.all().map((context) => glue.contexts.destroy(context)));43 });4445 after(() => {46 return Promise.all(47 initialContexts.map(({ name, context }) => {48 glue.contexts.update(name, context);49 })50 );51 });5253 describe('when manipulated by current app ', function () {54 it('should throw when no arguments are passed', (done) => {55 try {56 glue.contexts.update();57 done('Should throw an error');58 } catch (error) {59 done();60 }61 });6263 it('should throw when only one argument is passed', (done) => {64 try {65 glue.contexts.update(gtf.contexts.getContextName());66 done('Should throw an error');67 } catch (error) {68 done();69 }70 });7172 it('should not throw when 2 valid arguments are passed', async () => {73 let randomContextName = gtf.contexts.getContextName();74 let complexObject = gtf.contexts.generateComplexObject(10);75 await glue.contexts.update(randomContextName, complexObject);76 });7778 it('should not throw when more than 2 valid arguments are passed', async () => {79 let randomContextName = gtf.contexts.getContextName();80 let complexObject = gtf.contexts.generateComplexObject(10);81 await glue.contexts.update(randomContextName, complexObject, 'Tick42');82 });8384 [undefined, null, false, true, '', 42, [], { tick: 42 }, ['string']].forEach((invalidName) => {85 it(`should throw when an invalid first argument (${JSON.stringify(invalidName)}) is passed`, (done) => {86 const complexObject = gtf.contexts.generateComplexObject(10);8788 try {89 glue.contexts.update(invalidName, complexObject);90 done('Should throw an error');91 } catch (error) {92 done();93 }94 });95 });9697 [undefined, false, true, '', 42].forEach((invalidData) => {98 it(`should throw when an invalid second argument (${JSON.stringify(invalidData)}) is passed`, (done) => {99 try {100 glue.contexts.update(gtf.contexts.getContextName(), invalidData);101 done('Should throw an error');102 } catch (error) {103 done();104 }105 });106 });107108 it.skip('should throw when invoked with null as a second argument', (done) => {109 try {110 glue.contexts.update(gtf.contexts.getContextName(), null);111 done('Should throw an error');112 } catch (error) {113 done();114 }115 });116117 it.skip('should throw when invoked with empty array as a second argument', (done) => {118 try {119 glue.contexts.update(gtf.contexts.getContextName(), []);120 done('Should throw an error');121 } catch (error) {122 done();123 }124 });125126 it.skip('should return undefined', async () => {127 const context = {128 name: gtf.contexts.getContextName(),129 complexObj: gtf.contexts.generateComplexObject(10),130 };131132 const returned = await glue.contexts.update(context.name, context);133 expect(returned).to.eql(undefined);134 });135136 it("should create a new context when there isn't one with the given context name", async () => {137 const contextName = gtf.contexts.getContextName();138 const complexObject = gtf.contexts.generateComplexObject(10);139140 const contextAlreadyExists = glue.contexts.all().includes(contextName);141142 if (!contextAlreadyExists) {143 await glue.contexts.update(contextName, complexObject);144 } else {145 throw new Error('Invalid test - context already exists!');146 }147148 const newContext = await glue.contexts.get(contextName);149 expect(newContext).to.eql(complexObject);150 });151152 it("should create 3 more elements when there aren't any contexts with passed names", async () => {153 const oldLength = glue.contexts.all().length;154 await glue.contexts.update(gtf.contexts.getContextName(), {});155 await glue.contexts.update(gtf.contexts.getContextName(), {});156 await glue.contexts.update(gtf.contexts.getContextName(), {});157 const newLength = glue.contexts.all().length;158159 expect(oldLength + 3).to.eql(newLength);160 });161162 it("should add one more element to the array returned from all() when there isn't a context with passed name", async () => {163 const randomContextName = gtf.contexts.getContextName();164 const complexObject = gtf.contexts.generateComplexObject(10);165166 const oldLength = glue.contexts.all().length;167 await glue.contexts.update(randomContextName, complexObject);168 const newLength = glue.contexts.all().length;169170 expect(oldLength + 1).to.eql(newLength);171 });172173 it('should not add one more element to the array returned from all() when there is a context with passed name', async () => {174 const randomContextName = gtf.contexts.getContextName();175 const complexObject = gtf.contexts.generateComplexObject(10);176177 await glue.contexts.update(randomContextName, complexObject);178 const oldContextsLength = glue.contexts.all().length;179180 await glue.contexts.update(randomContextName, { newProp: 'value' });181 const newContextsLength = glue.contexts.all().length;182183 expect(oldContextsLength).to.eql(newContextsLength);184 });185186 it('should add new context keys to an existing context when invoked with valid name and data', async () => {187 const randomContextName = gtf.contexts.getContextName();188 const complexObject = gtf.contexts.generateComplexObject(10);189190 await glue.contexts.update(randomContextName, complexObject);191 const initialKeysLength = Object.keys(await glue.contexts.get(randomContextName)).length;192193 const newContext = {194 unique: 'unique',195 title: randomContextName,196 };197198 await glue.contexts.update(randomContextName, newContext);199200 const newKeysLength = Object.keys(await glue.contexts.get(randomContextName)).length;201202 expect(initialKeysLength + 2).to.eql(newKeysLength);203 expect([...Object.keys(complexObject), ...Object.keys(newContext)]).to.eql(Object.keys(await glue.contexts.get(randomContextName)));204 });205206 it('should update already existing key values when invoked with valid name and data', async () => {207 const context = {208 id: 'unique identifier',209 name: gtf.contexts.getContextName(),210 complexObject: gtf.contexts.generateComplexObject(10),211 };212 await glue.contexts.update(context.name, context);213 const newId = { id: 'NEW unique identifier' };214 await glue.contexts.update(context.name, newId);215 const updatedContext = await glue.contexts.get(context.name);216217 expect(updatedContext.id).to.eql(newId.id);218 });219220 it('should remove context keys when passed data has already existing keys set to null', async () => {221 const context = {222 id: 'unique identifier',223 name: gtf.contexts.getContextName(),224 complexObject: gtf.contexts.generateComplexObject(10),225 };226227 await glue.contexts.update(context.name, context);228 const initialKeys = Object.keys(await glue.contexts.get(context.name));229 const keyToRemove = { id: null };230 await glue.contexts.update(context.name, keyToRemove);231 const newKeys = Object.keys(await glue.contexts.get(context.name));232233 expect(newKeys.includes('id')).to.be.false;234 expect(initialKeys.length - 1).to.eql(newKeys.length);235 });236237 it('should create a new complex object when invoked with valid name and complex data', async () => {238 const complexContext = {239 name: gtf.contexts.getContextName(),240 complexObject: gtf.contexts.generateComplexObject(200),241 };242 await glue.contexts.update(complexContext.name, complexContext);243244 const newContext = await glue.contexts.get(complexContext.name);245 expect(newContext).to.eql(complexContext);246 });247248 it('should update an existing complex context with another complex object', async () => {249 const contextName = gtf.contexts.getContextName();250 const initComplexContext = {251 complexObject: gtf.contexts.generateComplexObject(200),252 };253 await glue.contexts.update(contextName, initComplexContext);254255 const newComplexContext = {256 newComplexObj: gtf.contexts.generateComplexObject(200),257 };258 await glue.contexts.update(contextName, newComplexContext);259260 const newContext = await glue.contexts.get(contextName);261 expect(newContext).to.eql(Object.assign({}, initComplexContext, newComplexContext));262 });263264 it("should invoke the callback registered with subscribe() method when there's a subscription for the context", (done) => {265 const contextName = gtf.contexts.getContextName();266267 glue.contexts268 .subscribe(contextName, () => {269 done();270 })271 .then(() => glue.contexts.update(contextName, {}));272 });273274 it("should invoke the callback registered with subscribe() method with the correct data as first argument when there's a subscription for the context", (done) => {275 const context = {276 name: gtf.contexts.getContextName(),277 complexObj: gtf.contexts.generateComplexObject(10),278 };279280 glue.contexts281 .subscribe(context.name, (updatedObject) => {282 try {283 expect(updatedObject).to.eql(context);284 done();285 } catch (error) {286 done(error);287 }288 })289 .then(() => glue.contexts.update(context.name, context))290 .catch((err) => done(err));291 });292293 it('should invoke the callback registered with subscribe() method 3 times when a context is changed 3 times', async () => {294 let counter = 0;295296 const context = {297 name: gtf.contexts.getContextName(),298 complexObj: gtf.contexts.generateComplexObject(10),299 };300301 await glue.contexts.subscribe(context.name, () => {302 counter++;303 });304305 await glue.contexts.update(context.name, context);306 await glue.contexts.update(context.name, { date: new Date() });307 await glue.contexts.update(context.name, { prop: 'value' });308309 expect(counter).to.eql(3);310 });311 });312313 describe('when manipulated by another app ', function () {314 let secondApp;315316 beforeEach(async () => {317 secondApp = await gtf.createApp();318 });319320 afterEach(() => secondApp.stop());321322 it("should add one more element to the array returned from all() when there isn't a context with passed name", async () => {323 const randomContextName = gtf.contexts.getContextName();324 const complexObject = gtf.contexts.generateComplexObject(10);325326 const oldLength = glue.contexts.all().length;327 await secondApp.contexts.update(randomContextName, complexObject);328 const newLength = glue.contexts.all().length;329330 expect(oldLength + 1).to.eql(newLength);331 });332333 it('should not add one more element to the array returned from all() when there is a context with passed name', async () => {334 const randomContextName = gtf.contexts.getContextName();335 const complexObject = gtf.contexts.generateComplexObject(10);336337 await glue.contexts.update(randomContextName, complexObject);338 const oldContextsLength = glue.contexts.all().length;339340 await secondApp.contexts.update(randomContextName, { newProp: 'value' });341 const newContextsLength = glue.contexts.all().length;342343 expect(oldContextsLength).to.eql(newContextsLength);344 });345346 it('should add new context keys to an existing context created by another app when invoked with valid name and data', async () => {347 const randomContextName = gtf.contexts.getContextName();348 const complexObject = gtf.contexts.generateComplexObject(10);349350 await glue.contexts.update(randomContextName, complexObject);351 const initialKeysLength = Object.keys(await glue.contexts.get(randomContextName)).length;352353 const newContext = {354 unique: 'unique',355 title: randomContextName,356 };357358 await secondApp.contexts.update(randomContextName, newContext);359360 const newKeysLength = Object.keys(await glue.contexts.get(randomContextName)).length;361362 expect(initialKeysLength + 2).to.eql(newKeysLength);363 expect([...Object.keys(complexObject), ...Object.keys(newContext)]).to.eql(Object.keys(await glue.contexts.get(randomContextName)));364 });365366 it('should update already existing key values of a context created by another app when invoked with valid name and data', async () => {367 const context = {368 id: 'unique identifier',369 name: gtf.contexts.getContextName(),370 complexObject: gtf.contexts.generateComplexObject(10),371 };372 await glue.contexts.update(context.name, context);373 const newId = { id: 'NEW unique identifier' };374 await secondApp.contexts.update(context.name, newId);375 const updatedContext = await glue.contexts.get(context.name);376377 expect(updatedContext.id).to.eql(newId.id);378 });379380 it('should remove context keys of a context created by another app when passed data has already existing keys set to null', async () => {381 const context = {382 id: 'unique identifier',383 name: gtf.contexts.getContextName(),384 complexObject: gtf.contexts.generateComplexObject(10),385 };386387 await glue.contexts.update(context.name, context);388 const initialKeys = Object.keys(await glue.contexts.get(context.name));389 const keyToRemove = { id: null };390 await secondApp.contexts.update(context.name, keyToRemove);391 const newKeys = Object.keys(await glue.contexts.get(context.name));392393 expect(newKeys.includes('id')).to.be.false;394 expect(initialKeys.length - 1).to.eql(newKeys.length);395 });396397 it('should create a new complex object when invoked with valid name and complex data', async () => {398 const complexContext = {399 name: gtf.contexts.getContextName(),400 complexObject: gtf.contexts.generateComplexObject(200),401 };402 await secondApp.contexts.update(complexContext.name, complexContext);403404 const newContext = await glue.contexts.get(complexContext.name);405 expect(newContext).to.eql(complexContext);406 });407408 it('should update an existing complex context with another complex object', async () => {409 const contextName = gtf.contexts.getContextName();410 const initComplexContext = {411 complexObject: gtf.contexts.generateComplexObject(200),412 };413 await secondApp.contexts.update(contextName, initComplexContext);414415 const newComplexContext = {416 newComplexObj: gtf.contexts.generateComplexObject(200),417 };418 await secondApp.contexts.update(contextName, newComplexContext);419420 const newContext = await glue.contexts.get(contextName);421 expect(newContext).to.eql(Object.assign({}, initComplexContext, newComplexContext));422 });423424 it("should invoke the callback registered with subscribe() method when there's a subscription for the context", (done) => {425 const contextName = gtf.contexts.getContextName();426427 glue.contexts428 .subscribe(contextName, () => {429 done();430 })431 .then(() => secondApp.contexts.update(contextName, {}));432 });433434 it("should invoke the callback registered with subscribe() method with the correct data as first argument when there's a subscription for the context", (done) => {435 const context = {436 name: gtf.contexts.getContextName(),437 complexObj: gtf.contexts.generateComplexObject(10),438 };439440 glue.contexts441 .subscribe(context.name, (updatedObject) => {442 try {443 expect(updatedObject).to.eql(context);444 done();445 } catch (error) {446 done(error);447 }448 })449 .then(() => secondApp.contexts.update(context.name, context))450 .catch((err) => done(err));451 });452453 it('should invoke the callback registered with subscribe() method 3 times when a context is changed 3 times', async () => {454 let counter = 0;455456 const context = {457 name: gtf.contexts.getContextName(),458 complexObj: gtf.contexts.generateComplexObject(10),459 };460461 await glue.contexts.subscribe(context.name, () => {462 counter++;463 });464465 await secondApp.contexts.update(context.name, context);466 await secondApp.contexts.update(context.name, { date: new Date() });467 await secondApp.contexts.update(context.name, { prop: 'value' });468469 expect(counter).to.eql(3);470 });471472 contextsForTesting.forEach((context) => {473 it(`should update the context between 2 applications with ${context.type}`, async () => {474 const contextName = gtf.contexts.getContextName();475 await glue.contexts.update(contextName, context);476477 const contextFromSecondApp = await secondApp.contexts.get(contextName);478479 expect(contextFromSecondApp).to.eql(context);480 });481 });482483 it('should merge the contexts when top level properties are the same', async () => {484 const contextName = gtf.contexts.getContextName();485 const firstContext = {486 a: 1,487 b: 2,488 };489490 const secondContext = {491 b: 0,492 c: 3,493 };494495 await glue.contexts.update(contextName, firstContext);496 await glue.contexts.update(contextName, secondContext);497498 const contextFromSecondApp = await secondApp.contexts.get(contextName);499500 expect(contextFromSecondApp.a).to.eql(firstContext.a);501 expect(contextFromSecondApp.b).to.eql(secondContext.b);502 expect(contextFromSecondApp.c).to.eql(secondContext.c);503 });504505 it.skip('should merge the contexts when inner properties are affected', async () => {506 const contextName = gtf.contexts.getContextName();507 const firstContext = {508 first: {509 a: 1,510 b: 2,511 },512 };513514 const secondContext = {515 first: {516 b: 0,517 c: 3,518 },519 };520521 await glue.contexts.update(contextName, firstContext);522 await glue.contexts.update(contextName, secondContext);523524 const contextFromSecondApp = await secondApp.contexts.get(contextName);525526 expect(contextFromSecondApp.first.a).to.eql(firstContext.first.a);527 expect(contextFromSecondApp.first.b).to.eql(secondContext.first.b);528 expect(contextFromSecondApp.first.c).to.eql(secondContext.first.c);529 });530531 it(`should not replace the old context when the new context is ${context.type} and 2 applications are used`, async () => {532 const contextName = gtf.contexts.getContextName();533 const initialContext = {534 isSaved: true,535 };536537 await glue.contexts.update(contextName, initialContext);538 await glue.contexts.update(contextName, { context: 'context' });539540 const contextFromSecondApp = await secondApp.contexts.get(contextName);541542 expect(contextFromSecondApp.isSaved).to.be.true;543 });544545 it.skip('should merge the contexts when top level array is affected', async () => {546 const contextName = gtf.contexts.getContextName();547 const firstContext = {548 a: [1],549 b: [2],550 };551552 const secondContext = {553 b: [0],554 c: [3],555 };556557 await glue.contexts.update(contextName, firstContext);558 await glue.contexts.update(contextName, secondContext);559560 const contextFromSecondApp = await secondApp.contexts.get(contextName);561562 expect(contextFromSecondApp.a).to.eql(firstContext.a);563 expect(contextFromSecondApp.b).to.eql([...secondContext.b, ...firstContext.b]);564 expect(contextFromSecondApp.c).to.eql(secondContext.c);565 });566567 it.skip('should merge the contexts when an inner array is affected', async () => {568 const firstContext = {569 first: {570 a: [1],571 b: [2],572 },573 };574575 const secondContext = {576 first: {577 b: [0],578 c: [3],579 },580 };581582 await glue.contexts.update(contextName, firstContext);583 await glue.contexts.update(contextName, secondContext);584585 const contextFromSecondApp = await secondApp.contexts.get(contextName);586587 expect(contextFromSecondApp.first.a).to.eql(firstContext.first.a);588 expect(contextFromSecondApp.first.b).to.eql([...secondContext.first.b, ...firstContext.first.b]);589 expect(contextFromSecondApp.first.c).to.eql(secondContext.first.c);590 });591 }); ...

Full Screen

Full Screen

set.spec.js

Source:set.spec.js Github

copy

Full Screen

1describe('set() ', function () {2 const contextsForTesting = [3 { type: 'simple' },4 {5 type: 'simpleWithArray',6 arr: [1, 2, 3],7 },8 {9 type: 'arrayWithDifferentTypes',10 arr: [1, '2', {}],11 },12 {13 type: 'nestedObjects',14 first: {15 second: {16 third: {},17 },18 },19 },20 {21 type: 'cubeRepresentation',22 cube: [[[1]], [[2]], [[3]]],23 },24 ];2526 let initialContexts;2728 before(async () => {29 await coreReady;3031 initialContexts = await Promise.all(32 glue.contexts.all().map((contextName) => {33 const context = glue.contexts.get(contextName);34 return { name: contextName, context };35 })36 );3738 return Promise.all(glue.contexts.all().map((contextName) => glue.contexts.destroy(contextName)));39 });4041 afterEach(() => {42 gtf.clearWindowActiveHooks();4344 return Promise.all(glue.contexts.all().map((context) => glue.contexts.destroy(context)));45 });4647 after(() => {48 return Promise.all(49 initialContexts.map(({ name, context }) => {50 glue.contexts.update(name, context);51 })52 );53 });5455 describe('when manipulated by current app ', function () {56 it('should throw when no arguments are passed', (done) => {57 try {58 glue.contexts.set();59 done('Should throw an error!');60 } catch (error) {61 done();62 }63 });6465 it('should throw when only one argument is passed', (done) => {66 try {67 glue.contexts.set(gtf.contexts.getContextName());68 done('Should throw an error!');69 } catch (error) {70 done();71 }72 });7374 it('should not throw when 2 valid arguments are passed', async () => {75 let randomContextName = gtf.contexts.getContextName();76 let complexObject = gtf.contexts.generateComplexObject(10);77 await glue.contexts.set(randomContextName, complexObject);78 });7980 it('should not throw when more than 2 valid arguments are passed', async () => {81 let randomContextName = gtf.contexts.getContextName();82 let complexObject = gtf.contexts.generateComplexObject(10);83 await glue.contexts.set(randomContextName, complexObject, 'third argument');84 });8586 [undefined, null, false, true, '', 42, [], { tick: 42 }].forEach((invalidName) => {87 it(`should throw when an invalid first argument (${JSON.stringify(invalidName)}) is passed`, (done) => {88 const complexObject = gtf.contexts.generateComplexObject(10);8990 try {91 glue.contexts.set(invalidName, complexObject);92 done('Should throw an error');93 } catch (error) {94 done();95 }96 });97 });9899 [undefined, false, true, '', 42].forEach((invalidData) => {100 it(`should throw when an invalid second argument (${JSON.stringify(invalidData)}) is passed`, (done) => {101 try {102 glue.contexts.set(gtf.contexts.getContextName(), invalidData);103 done('Should throw an error');104 } catch (error) {105 done();106 }107 });108 });109110 it.skip('should throw when invoked with null as a second argument', (done) => {111 try {112 glue.contexts.set(gtf.contexts.getContextName(), null);113 done('Should throw an error');114 } catch (error) {115 done();116 }117 });118119 it.skip('should throw when invoked with empty array as a second argument', (done) => {120 try {121 glue.contexts.set(gtf.contexts.getContextName(), []);122 done('Should throw an error');123 } catch (error) {124 done();125 }126 });127128 it.skip('should return undefined', async () => {129 const context = {130 name: gtf.contexts.getContextName(),131 complexObj: gtf.contexts.generateComplexObject(10),132 };133134 const returned = await glue.contexts.set(context.name, context);135 expect(returned).to.eql(undefined);136 });137138 it("should create a new context if there isn't one with the given name", async () => {139 const contextName = gtf.contexts.getContextName();140 const complexObject = gtf.contexts.generateComplexObject(10);141142 const contextAlreadyExists = glue.contexts.all().includes(contextName);143144 if (!contextAlreadyExists) {145 await glue.contexts.set(contextName, complexObject);146 } else {147 throw new Error('Invalid test - context already exists!');148 }149150 const newContext = await glue.contexts.get(contextName);151 expect(newContext).to.eql(complexObject);152 });153154 it("should create 3 more elements when there aren't any contexts with passed names", async () => {155 const oldLength = glue.contexts.all().length;156 await glue.contexts.set(gtf.contexts.getContextName(), {});157 await glue.contexts.set(gtf.contexts.getContextName(), {});158 await glue.contexts.set(gtf.contexts.getContextName(), {});159 const newLength = glue.contexts.all().length;160161 expect(oldLength + 3).to.eql(newLength);162 });163164 it("should add one more element to the array returned from all() when there isn't a context with passed name", async () => {165 const randomContextName = gtf.contexts.getContextName();166 const complexObject = gtf.contexts.generateComplexObject(10);167168 const oldLength = glue.contexts.all().length;169 await glue.contexts.set(randomContextName, complexObject);170 const newLength = glue.contexts.all().length;171172 expect(oldLength + 1).to.eql(newLength);173 });174175 it('should not add one more element to the array returned from all() when there is a context with passed name', async () => {176 const randomContextName = gtf.contexts.getContextName();177 const complexObject = gtf.contexts.generateComplexObject(10);178179 await glue.contexts.set(randomContextName, complexObject);180 const oldContextsLength = glue.contexts.all().length;181182 await glue.contexts.set(randomContextName, { newProp: 'value' });183 const newContextsLength = glue.contexts.all().length;184185 expect(oldContextsLength).to.eql(newContextsLength);186 });187188 it('should remove all previous context properties', async () => {189 const contextName = gtf.contexts.getContextName();190 const initContext = {191 id: gtf.contexts.getContextName(),192 name: 'initial Context',193 complexObject: gtf.contexts.generateComplexObject(10),194 };195196 await glue.contexts.set(contextName, initContext);197 const initialKeys = Object.keys(await glue.contexts.get(contextName));198199 const newContext = {200 product: 'GTF',201 company: 'Tick42',202 };203204 await glue.contexts.set(contextName, newContext);205 const newKeys = Object.keys(await glue.contexts.get(contextName));206207 const repetitiveKeys = initialKeys.filter((key) => newKeys.includes(key));208209 expect(repetitiveKeys).to.eql([]);210 });211212 it('should replace the context with the passed object when invoked with valid name and data', async () => {213 const contextName = gtf.contexts.getContextName();214 const initContext = {215 id: 'unique identifier',216 complexObject: gtf.contexts.generateComplexObject(10),217 };218219 await glue.contexts.set(contextName, initContext);220221 const newContext = {222 name: 'new context',223 title: 'test',224 };225226 await glue.contexts.set(contextName, newContext);227228 const currentContext = await glue.contexts.get(contextName);229 expect(currentContext).to.eql(newContext);230 });231232 it('should create a new complex object when invoked with valid name and complex data', async () => {233 const complexContext = {234 name: gtf.contexts.getContextName(),235 complexObject: gtf.contexts.generateComplexObject(200),236 };237238 await glue.contexts.set(complexContext.name, complexContext);239240 const currentContext = await glue.contexts.get(complexContext.name);241 expect(currentContext).to.eql(complexContext);242 });243244 it('should replace an existing complex context with another complex object', async () => {245 const contextName = gtf.contexts.getContextName();246 const initComplexContext = {247 complexObject: gtf.contexts.generateComplexObject(200),248 };249 await glue.contexts.set(contextName, initComplexContext);250251 const newComplexContext = {252 newComplexObj: gtf.contexts.generateComplexObject(200),253 };254 await glue.contexts.set(contextName, newComplexContext);255256 const currentContext = await glue.contexts.get(contextName);257 expect(currentContext).to.eql(newComplexContext);258 });259260 it("should invoke the callback registered with subscribe() method when there's a subscription for the context", (done) => {261 const contextName = gtf.contexts.getContextName();262263 glue.contexts264 .subscribe(contextName, () => {265 done();266 })267 .then(() => glue.contexts.set(contextName, {}));268 });269270 it("should invoke the callback registered with subscribe() method with the correct data as first argument when there's a subscription for the context", (done) => {271 const context = {272 name: gtf.contexts.getContextName(),273 complexObj: gtf.contexts.generateComplexObject(10),274 };275276 glue.contexts277 .subscribe(context.name, (updatedObject) => {278 try {279 expect(updatedObject).to.eql(context);280 done();281 } catch (error) {282 done(error)283 }284285 })286 .then(() => glue.contexts.set(context.name, context))287 .catch((err) => done(err));288 });289290 it('should invoke the callback registered with subscribe() method 3 times when a context is changed 3 times', async () => {291 let counter = 0;292293 const context = {294 name: gtf.contexts.getContextName(),295 complexObj: gtf.contexts.generateComplexObject(10),296 };297298 await glue.contexts.subscribe(context.name, () => {299 counter++;300 });301302 await glue.contexts.set(context.name, context);303 await glue.contexts.set(context.name, { date: new Date() });304 await glue.contexts.set(context.name, { prop: 'value' });305306 expect(counter).to.eql(3);307 });308 });309310 describe('when manipulated by another app ', function () {311 let secondApp;312313 beforeEach(async () => {314 secondApp = await gtf.createApp();315 });316317 afterEach(() => secondApp.stop());318319 it("should create a new context if there isn't one with the given name", async () => {320 const contextName = gtf.contexts.getContextName();321 const complexObject = gtf.contexts.generateComplexObject(10);322323 const contextAlreadyExists = glue.contexts.all().includes(contextName);324325 if (!contextAlreadyExists) {326 await secondApp.contexts.set(contextName, complexObject);327 } else {328 throw new Error('Invalid test - context already exists!');329 }330331 const newContext = await glue.contexts.get(contextName);332 expect(newContext).to.eql(complexObject);333 });334335 it("should create 3 more elements when there aren't any contexts with passed names", async () => {336 const oldLength = glue.contexts.all().length;337 await secondApp.contexts.set(gtf.contexts.getContextName(), {});338 await secondApp.contexts.set(gtf.contexts.getContextName(), {});339 await secondApp.contexts.set(gtf.contexts.getContextName(), {});340 const newLength = glue.contexts.all().length;341342 expect(oldLength + 3).to.eql(newLength);343 });344345 it("should add one more element to the array returned from all() when there isn't a context with passed name", async () => {346 const randomContextName = gtf.contexts.getContextName();347 const complexObject = gtf.contexts.generateComplexObject(10);348349 const oldLength = glue.contexts.all().length;350 await secondApp.contexts.set(randomContextName, complexObject);351 const newLength = glue.contexts.all().length;352353 expect(oldLength + 1).to.eql(newLength);354 });355356 it('should not add one more element to the array returned from all() when there is a context with passed name', async () => {357 const randomContextName = gtf.contexts.getContextName();358 const complexObject = gtf.contexts.generateComplexObject(10);359360 await glue.contexts.set(randomContextName, complexObject);361 const oldContextsLength = glue.contexts.all().length;362363 await secondApp.contexts.set(randomContextName, { newProp: 'value' });364 const newContextsLength = glue.contexts.all().length;365366 expect(oldContextsLength).to.eql(newContextsLength);367 });368369 it('should remove all previous context properties', async () => {370 const contextName = gtf.contexts.getContextName();371 const initContext = {372 id: gtf.contexts.getContextName(),373 name: 'initial Context',374 complexObject: gtf.contexts.generateComplexObject(10),375 };376377 await secondApp.contexts.set(contextName, initContext);378 const initialKeys = Object.keys(await glue.contexts.get(contextName));379380 const newContext = {381 product: 'GTF',382 company: 'Tick42',383 };384385 await secondApp.contexts.set(contextName, newContext);386 const newKeys = Object.keys(await glue.contexts.get(contextName));387388 const repetitiveKeys = initialKeys.filter((key) => newKeys.includes(key));389390 expect(repetitiveKeys).to.eql([]);391 });392393 it('should replace the context with the passed object when invoked with valid name and data', async () => {394 const contextName = gtf.contexts.getContextName();395 const initContext = {396 id: 'unique identifier',397 complexObject: gtf.contexts.generateComplexObject(10),398 };399400 await secondApp.contexts.set(contextName, initContext);401402 const newContext = {403 name: 'new context',404 title: 'test',405 };406407 await secondApp.contexts.set(contextName, newContext);408409 const currentContext = await glue.contexts.get(contextName);410 expect(currentContext).to.eql(newContext);411 });412413 it('should create a new complex object when invoked with valid name and complex data', async () => {414 const complexContext = {415 name: gtf.contexts.getContextName(),416 complexObject: gtf.contexts.generateComplexObject(200),417 };418419 await secondApp.contexts.set(complexContext.name, complexContext);420421 const newContext = await glue.contexts.get(complexContext.name);422 expect(newContext).to.eql(complexContext);423 });424425 it('should replace an existing complex context with another complex object', async () => {426 const contextName = gtf.contexts.getContextName();427 const initComplexContext = {428 complexObject: gtf.contexts.generateComplexObject(200),429 };430 await secondApp.contexts.set(contextName, initComplexContext);431432 const newComplexContext = {433 newComplexObj: gtf.contexts.generateComplexObject(200),434 };435 await secondApp.contexts.set(contextName, newComplexContext);436437 const currentContext = await glue.contexts.get(contextName);438 expect(currentContext).to.eql(newComplexContext);439 });440441 it("should invoke the callback registered with subscribe() method when there's a subscription for the context", (done) => {442 const contextName = gtf.contexts.getContextName();443444 glue.contexts445 .subscribe(contextName, () => {446 done();447 })448 .then(() => secondApp.contexts.set(contextName, {}));449 });450451 it("should invoke the callback registered with subscribe() method with the correct data as first argument when there's a subscription for the context", (done) => {452 const context = {453 name: gtf.contexts.getContextName(),454 complexObj: gtf.contexts.generateComplexObject(10),455 };456457 glue.contexts458 .subscribe(context.name, (updatedObject) => {459 try {460 expect(updatedObject).to.eql(context);461 done();462 } catch (error) {463 done(error);464 }465 })466 .then(() => secondApp.contexts.set(context.name, context))467 .catch((err) => done(err));468 });469470 it('should invoke the callback registered with subscribe() method 3 times when a context is changed 3 times', async () => {471 let counter = 0;472473 const context = {474 name: gtf.contexts.getContextName(),475 complexObj: gtf.contexts.generateComplexObject(10),476 };477478 await glue.contexts.subscribe(context.name, () => {479 counter++;480 });481482 await glue.contexts.set(context.name, context);483 await glue.contexts.set(context.name, { date: new Date() });484 await glue.contexts.set(context.name, { prop: 'value' });485486 expect(counter).to.eql(3);487 });488489 contextsForTesting.forEach((context) => {490 it(`should set the context between 2 applications with ${context.type}`, async () => {491 const contextName = gtf.contexts.getContextName();492 await glue.contexts.set(contextName, context);493494 const contextFromSecondApp = await secondApp.contexts.get(contextName);495496 expect(contextFromSecondApp).to.eql(context);497 });498499 it(`should replace the old context when the new context is ${context.type} and 2 applications are used`, async () => {500 const contextName = gtf.contexts.getContextName();501 const initialContext = {502 isSaved: true,503 };504505 await glue.contexts.set(contextName, initialContext);506 await glue.contexts.set(contextName, context);507508 const contextFromSecondApp = await secondApp.contexts.get(contextName);509510 expect(contextFromSecondApp.isSaved).to.be.undefined;511 });512 });513514 it('should replace the context when top level properties are the same', async () => {515 const contextName = gtf.contexts.getContextName();516 const firstContext = {517 a: 1,518 b: 2,519 };520521 const secondContext = {522 b: 0,523 c: 3,524 };525526 await glue.contexts.set(contextName, firstContext);527 await glue.contexts.set(contextName, secondContext);528529 const contextFromSecondApp = await secondApp.contexts.get(contextName);530531 expect(contextFromSecondApp).to.eql(secondContext);532 });533534 it('should replace the context when inner properties are affected', async () => {535 const contextName = gtf.contexts.getContextName();536 const firstContext = {537 first: {538 a: 1,539 b: 2,540 },541 };542543 const secondContext = {544 first: {545 b: 0,546 c: 3,547 },548 };549 await glue.contexts.set(contextName, firstContext);550 await glue.contexts.set(contextName, secondContext);551552 const contextFromSecondApp = await secondApp.contexts.get(contextName);553554 expect(contextFromSecondApp).to.eql(secondContext);555 });556557 it('should replace the context when top level array is affected', async () => {558 const contextName = gtf.contexts.getContextName();559 const firstContext = {560 a: [1],561 b: [2],562 };563564 const secondContext = {565 b: [0],566 c: [3],567 };568 await glue.contexts.set(contextName, firstContext);569 await glue.contexts.set(contextName, secondContext);570571 const contextFromSecondApp = await secondApp.contexts.get(contextName);572573 expect(contextFromSecondApp).to.eql(secondContext);574 });575576 it('should replace the context when an inner array is affected', async () => {577 const contextName = gtf.contexts.getContextName();578 const firstContext = {579 first: {580 a: [1],581 b: [2],582 },583 };584585 const secondContext = {586 first: {587 b: [0],588 c: [3],589 },590 };591 await glue.contexts.set(contextName, firstContext);592 await glue.contexts.set(contextName, secondContext);593594 const contextFromSecondApp = await secondApp.contexts.get(contextName);595596 expect(contextFromSecondApp).to.eql(secondContext);597 });598 }); ...

Full Screen

Full Screen

setPaths.spec.js

Source:setPaths.spec.js Github

copy

Full Screen

1describe('setPaths() ', function () {2 let initialContexts;3 before(async () => {4 await coreReady;5 initialContexts = await Promise.all(6 glue.contexts.all().map((contextName) => {7 const context = glue.contexts.get(contextName);8 return { name: contextName, context };9 })10 );11 return Promise.all(glue.contexts.all().map((context) => glue.contexts.destroy(context)));12 });13 afterEach(() => {14 gtf.clearWindowActiveHooks();15 return Promise.all(glue.contexts.all().map((context) => glue.contexts.destroy(context)));16 });17 after(() => {18 return Promise.all(19 initialContexts.map(({ name, context }) => {20 glue.contexts.update(name, context);21 })22 );23 });24 describe('when manipulated by current app ', function () {25 it('should throw when no arguments are passed', (done) => {26 try {27 glue.contexts.setPaths();28 done('Should throw an error');29 } catch (error) {30 done();31 }32 });33 it('should throw when only one argument is passed', (done) => {34 try {35 glue.contexts.setPaths('contextName');36 done('Should throw an error');37 } catch (error) {38 done();39 }40 });41 [undefined, null, false, true, 42, [], '', { tick: 42 }].forEach((invalidName) => {42 it(`should throw when an invalid first argument (${JSON.stringify(invalidName)}) is passed`, (done) => {43 try {44 glue.contexts.setPaths(invalidName, [{ path: 'prop1', value: 'value' }]);45 done('Should throw an error');46 } catch (error) {47 done();48 }49 });50 });51 [undefined, null, false, true, 42, '', { tick: 42 }, [{ tick: 42 }]].forEach((invalidPaths) => {52 it(`should throw when an invalid argument (${JSON.stringify(invalidPaths)}) is passed`, (done) => {53 try {54 glue.contexts.setPaths('contextName', invalidPaths);55 done('Should throw an error');56 } catch (error) {57 done();58 }59 });60 });61 it.skip('should return undefined', async () => {62 const returned = await glue.contexts.setPaths('contextName', [63 { path: 'prop1', value: 'value' },64 { path: 'prop2', value: 'value' },65 ]);66 expect(returned).to.eql(undefined);67 });68 it('should not throw when invoked with valid arguments', (done) => {69 try {70 glue.contexts.setPaths('contextName', [{ path: 'prop1.prop2', value: 'value' }]);71 done();72 } catch (error) {73 done(error);74 }75 });76 it("should create a new context when there isn't one with the given context name", async () => {77 const contextName = gtf.contexts.getContextName();78 await glue.contexts.setPaths(contextName, [{ path: 'prop', value: 'value' }]);79 const newContext = await glue.contexts.get(contextName);80 expect(newContext).to.eql({ prop: 'value' });81 });82 it("should add one more element to the array returned from all() when there isn't a context with passed name", async () => {83 const contextName = gtf.contexts.getContextName();84 const oldLength = glue.contexts.all().length;85 await glue.contexts.setPaths(contextName, [{ path: 'prop1', value: 'value' }]);86 const newLength = glue.contexts.all().length;87 expect(oldLength + 1).to.eql(newLength);88 });89 it('should not add one more element to the array returned from all() when there is a context with passed name', async () => {90 const contextName = gtf.contexts.getContextName();91 const complexObject = gtf.contexts.generateComplexObject(10);92 const anotherComplexObject = gtf.contexts.generateComplexObject(10);93 await glue.contexts.setPaths(contextName, [{ path: 'prop1.prop2', value: complexObject }]);94 const oldLength = glue.contexts.all().length;95 await glue.contexts.setPaths(contextName, [{ path: 'prop1.prop2', value: anotherComplexObject }]);96 const newLength = glue.contexts.all().length;97 expect(oldLength).to.eql(newLength);98 });99 it('should add new nested properties to the passed paths', async () => {100 const contextName = gtf.contexts.getContextName();101 const complexObject = gtf.contexts.generateComplexObject(10);102 const anotherComplexObject = gtf.contexts.generateComplexObject(10);103 await glue.contexts.setPaths(contextName, [104 { path: 'prop1.prop2', value: complexObject },105 { path: 'prop3.prop4', value: anotherComplexObject },106 ]);107 const newContext = await glue.contexts.get(contextName);108 expect(newContext).to.eql({109 prop1: { prop2: complexObject },110 prop3: { prop4: anotherComplexObject },111 });112 });113 it('should not change previous context properties', async () => {114 const contextName = gtf.contexts.getContextName();115 const context = gtf.contexts.generateComplexObject(10);116 const complexObject = gtf.contexts.generateComplexObject(10);117 await glue.contexts.update(contextName, context);118 await glue.contexts.setPaths(contextName, [{ path: 'prop1.prop2', value: complexObject }]);119 const newContext = await glue.contexts.get(contextName);120 expect(newContext).to.eql(121 Object.assign({}, context, {122 prop1: { prop2: complexObject },123 })124 );125 });126 it('should update already existing context key values when invoked with valid data', async () => {127 const contextName = gtf.contexts.getContextName();128 const initContext = gtf.contexts.generateComplexObject(10);129 await glue.contexts.setPaths(contextName, [{ path: 'path1.path2', value: initContext }]);130 const newContext = gtf.contexts.generateComplexObject(10);131 await glue.contexts.setPaths(contextName, [{ path: 'path1.path2', value: newContext }]);132 const currentContext = await glue.contexts.get(contextName);133 expect(currentContext).to.eql({134 path1: { path2: newContext },135 });136 });137 it('should remove context key when passed data has an already existing key set to null', async () => {138 const context = {139 id: 'unique identifier',140 name: gtf.contexts.getContextName(),141 complexObject: gtf.contexts.generateComplexObject(10),142 };143 await glue.contexts.update(context.name, context);144 await glue.contexts.setPaths(context.name, [{ path: 'prop1.prop2', value: 'prop2 value' }]);145 const initialKeys = Object.keys(await glue.contexts.get(context.name));146 await glue.contexts.setPaths(context.name, [{ path: 'prop1', value: null }]);147 const newKeys = Object.keys(await glue.contexts.get(context.name));148 expect(newKeys.includes('prop1')).to.be.false;149 expect(initialKeys.length - 1).to.eql(newKeys.length);150 });151 it('should create a new complex context when invoked with valid arguments', async () => {152 const contextName = gtf.contexts.getContextName();153 const complexObj = gtf.contexts.generateComplexObject(200);154 await glue.contexts.setPaths(contextName, [{ path: 'prop1', value: complexObj }]);155 const newContext = await glue.contexts.get(contextName);156 expect(newContext).to.eql({ prop1: complexObj });157 });158 it('should add new prop with complex data when updating an existing complex context', async () => {159 const contextName = gtf.contexts.getContextName();160 const complexObject = gtf.contexts.generateComplexObject();161 const anotherComplexObject = gtf.contexts.generateComplexObject();162 await glue.contexts.setPaths(contextName, [163 { path: 'prop1', value: complexObject },164 { path: 'prop2', value: anotherComplexObject },165 ]);166 const newContext = await glue.contexts.get(contextName);167 expect(newContext).to.eql({168 prop1: complexObject,169 prop2: anotherComplexObject,170 });171 });172 it('should replace an already existing complex property with another complex object', async () => {173 const contextName = gtf.contexts.getContextName();174 const prop2Context = {175 id: gtf.contexts.getContextName(),176 prop2: 'value',177 };178 const initComplexObj = gtf.contexts.generateComplexObject(200);179 const anotherComplexObj = gtf.contexts.generateComplexObject(200);180 await glue.contexts.setPaths(contextName, [181 { path: 'prop1', value: initComplexObj },182 { path: 'prop2', value: prop2Context },183 ]);184 await glue.contexts.setPaths(contextName, [{ path: 'prop1', value: anotherComplexObj }]);185 const newContext = await glue.contexts.get(contextName);186 expect(newContext).to.eql({ prop1: anotherComplexObj, prop2: prop2Context });187 });188 it("should invoke the callback registered with subscribe() method when there's a subscription for the context", (done) => {189 const contextName = gtf.contexts.getContextName();190 glue.contexts191 .subscribe(contextName, (updatedCtx) => {192 if (updatedCtx.prop) {193 done();194 }195 })196 .then((unsubscribeFn) => {197 gtf.addWindowHook(unsubscribeFn);198 return glue.contexts.setPaths(contextName, [{ path: 'prop', value: 'value' }]);199 })200 .catch((err) => done(err));201 });202 it("should invoke the callback registered with subscribe() method with the correct data as first argument when there's a subscription for the context", (done) => {203 const contextName = gtf.contexts.getContextName();204 const prop1Context = {205 id: gtf.contexts.getContextName(),206 complexObj: gtf.contexts.generateComplexObject(10),207 };208 const prop2Context = {209 title: 'prop2 context',210 value: 'prop2 value',211 };212 glue.contexts213 .subscribe(contextName, (updatedObject) => {214 if (Object.keys(updatedObject).length) {215 try {216 expect(updatedObject).to.eql({ prop1: prop1Context, prop2: prop2Context });217 done();218 } catch (err) {219 done(err);220 }221 }222 })223 .then((unsubscribeFn) => {224 gtf.addWindowHook(unsubscribeFn);225 return glue.contexts.setPaths(contextName, [226 { path: 'prop1', value: prop1Context },227 { path: 'prop2', value: prop2Context },228 ]);229 })230 .catch((err) => done(err));231 });232 it('should invoke the callback registered with subscribe() method 3 times when a context is updated 3 times', (done) => {233 const contextName = gtf.contexts.getContextName();234 const prop1Context = {235 id: gtf.contexts.getContextName(),236 complexObj: gtf.contexts.generateComplexObject(10),237 };238 const prop2Context = {239 title: 'prop2 context',240 value: 'prop2 value',241 };242 const ready = gtf.waitFor(3, done);243 glue.contexts244 .subscribe(contextName, (data) => {245 if (data.prop1.id === prop1Context.id) {246 ready();247 }248 })249 .then((unsubscribeFn) => {250 gtf.addWindowHook(unsubscribeFn);251 return glue.contexts.setPaths(contextName, [{ path: 'prop1', value: prop1Context }]);252 })253 .then(() => glue.contexts.setPaths(contextName, [{ path: 'prop2', value: prop2Context }]))254 .then(() => glue.contexts.setPaths(contextName, [{ path: 'prop3', value: 'value3' }]))255 .catch((err) => done(err));256 });257 });258 describe('when manipulated by another app, ', function () {259 let supportApp;260 beforeEach(async () => {261 supportApp = await gtf.createApp();262 });263 afterEach(() => supportApp.stop());264 it("should create a new context when there isn't one with the given context name", async () => {265 const contextName = gtf.contexts.getContextName();266 await supportApp.contexts.setPaths(contextName, [{ path: 'prop', value: 'value' }]);267 const newContext = await glue.contexts.get(contextName);268 expect(newContext).to.eql({ prop: 'value' });269 });270 it("should add one more element to the array returned from all() when there isn't a context with passed name", async () => {271 const contextName = gtf.contexts.getContextName();272 const oldLength = glue.contexts.all().length;273 await supportApp.contexts.setPaths(contextName, [{ path: 'prop1', value: 'value' }]);274 const newLength = glue.contexts.all().length;275 expect(oldLength + 1).to.eql(newLength);276 });277 it('should not add one more element to the array returned from all() when there is a context with passed name', async () => {278 const contextName = gtf.contexts.getContextName();279 const complexObject = gtf.contexts.generateComplexObject(10);280 const anotherComplexObject = gtf.contexts.generateComplexObject(10);281 await supportApp.contexts.setPaths(contextName, [{ path: 'prop1.prop2', value: complexObject }]);282 const oldLength = glue.contexts.all().length;283 await supportApp.contexts.setPaths(contextName, [{ path: 'prop1.prop2', value: anotherComplexObject }]);284 const newLength = glue.contexts.all().length;285 expect(oldLength).to.eql(newLength);286 });287 it('should add new nested properties to the passed paths', async () => {288 const contextName = gtf.contexts.getContextName();289 const complexObject = gtf.contexts.generateComplexObject(10);290 const anotherComplexObject = gtf.contexts.generateComplexObject(10);291 await supportApp.contexts.setPaths(contextName, [292 { path: 'prop1.prop2', value: complexObject },293 { path: 'prop3.prop4', value: anotherComplexObject },294 ]);295 const newContext = await glue.contexts.get(contextName);296 expect(newContext).to.eql({297 prop1: { prop2: complexObject },298 prop3: { prop4: anotherComplexObject },299 });300 });301 it('should not change previous context properties', async () => {302 const contextName = gtf.contexts.getContextName();303 const context = gtf.contexts.generateComplexObject(10);304 const complexObject = gtf.contexts.generateComplexObject(10);305 await glue.contexts.update(contextName, context);306 await supportApp.contexts.setPaths(contextName, [{ path: 'prop1.prop2', value: complexObject }]);307 const newContext = await glue.contexts.get(contextName);308 expect(newContext).to.eql(309 Object.assign({}, context, {310 prop1: { prop2: complexObject },311 })312 );313 });314 it('should update already existing context key values when invoked with valid data', async () => {315 const contextName = gtf.contexts.getContextName();316 const initContext = gtf.contexts.generateComplexObject(10);317 await glue.contexts.setPaths(contextName, [{ path: 'path1.path2', value: initContext }]);318 const newContext = gtf.contexts.generateComplexObject(10);319 await supportApp.contexts.setPaths(contextName, [{ path: 'path1.path2', value: newContext }]);320 const currentContext = await glue.contexts.get(contextName);321 expect(currentContext).to.eql({322 path1: { path2: newContext },323 });324 });325 it('should remove context key when passed data has an already existing key set to null', async () => {326 const context = {327 id: 'unique identifier',328 name: gtf.contexts.getContextName(),329 complexObject: gtf.contexts.generateComplexObject(10),330 };331 await glue.contexts.update(context.name, context);332 await glue.contexts.setPaths(context.name, [{ path: 'prop1.prop2', value: 'prop2 value' }]);333 const initialKeys = Object.keys(await glue.contexts.get(context.name));334 await supportApp.contexts.setPaths(context.name, [{ path: 'prop1', value: null }]);335 const newKeys = Object.keys(await glue.contexts.get(context.name));336 expect(newKeys.includes('prop1')).to.be.false;337 expect(initialKeys.length - 1).to.eql(newKeys.length);338 });339 it('should create a new complex context when invoked with valid arguments', async () => {340 const contextName = gtf.contexts.getContextName();341 const complexObj = gtf.contexts.generateComplexObject(200);342 await supportApp.contexts.setPaths(contextName, [{ path: 'prop1', value: complexObj }]);343 const newContext = await glue.contexts.get(contextName);344 expect(newContext).to.eql({ prop1: complexObj });345 });346 it('should add new prop with complex data when updating an existing complex context', async () => {347 const contextName = gtf.contexts.getContextName();348 const complexObject = gtf.contexts.generateComplexObject();349 const anotherComplexObject = gtf.contexts.generateComplexObject();350 await supportApp.contexts.setPaths(contextName, [351 { path: 'prop1', value: complexObject },352 { path: 'prop2', value: anotherComplexObject },353 ]);354 const newContext = await glue.contexts.get(contextName);355 expect(newContext).to.eql({356 prop1: complexObject,357 prop2: anotherComplexObject,358 });359 });360 it('should replace an already existing complex property with another complex object', async () => {361 const contextName = gtf.contexts.getContextName();362 const prop2Context = {363 id: gtf.contexts.getContextName(),364 prop2: 'value',365 };366 const initComplexObj = gtf.contexts.generateComplexObject(200);367 const anotherComplexObj = gtf.contexts.generateComplexObject(200);368 await glue.contexts.setPaths(contextName, [369 { path: 'prop1', value: initComplexObj },370 { path: 'prop2', value: prop2Context },371 ]);372 await supportApp.contexts.setPaths(contextName, [{ path: 'prop1', value: anotherComplexObj }]);373 const newContext = await glue.contexts.get(contextName);374 expect(newContext).to.eql({ prop1: anotherComplexObj, prop2: prop2Context });375 });376 it("should invoke the callback registered with subscribe() method when there's a subscription for the context", (done) => {377 const contextName = gtf.contexts.getContextName();378 glue.contexts379 .subscribe(contextName, () => {380 done();381 })382 .then((unsubscribeFn) => {383 gtf.addWindowHook(unsubscribeFn);384 return supportApp.contexts.setPaths(contextName, [{ path: 'prop', value: 'value' }]);385 })386 .catch((err) => done(err));387 });388 it("should invoke the callback registered with subscribe() method with the correct data as first argument when there's a subscription for the context", (done) => {389 const contextName = gtf.contexts.getContextName();390 const prop1Context = {391 id: gtf.contexts.getContextName(),392 complexObj: gtf.contexts.generateComplexObject(10),393 };394 const prop2Context = {395 title: 'prop2 context',396 value: 'prop2 value',397 };398 glue.contexts399 .subscribe(contextName, (updatedObject) => {400 if (Object.keys(updatedObject).length) {401 try {402 expect(updatedObject).to.eql({ prop1: prop1Context, prop2: prop2Context });403 done();404 } catch (err) {405 done(err);406 }407 }408 })409 .then(() =>410 supportApp.contexts.setPaths(contextName, [411 { path: 'prop1', value: prop1Context },412 { path: 'prop2', value: prop2Context },413 ])414 )415 .catch((err) => done(err));416 });417 it('should invoke the callback registered with subscribe() method 3 times when a context is updated 3 times', (done) => {418 const contextName = gtf.contexts.getContextName();419 const prop1Context = {420 id: gtf.contexts.getContextName(),421 complexObj: gtf.contexts.generateComplexObject(10),422 };423 const prop2Context = {424 title: 'prop2 context',425 value: 'prop2 value',426 };427 const ready = gtf.waitFor(3, done);428 glue.contexts429 .subscribe(contextName, (data) => {430 if (data.prop1.id === prop1Context.id) {431 ready();432 }433 })434 .then((unsubscribeFn) => {435 gtf.addWindowHook(unsubscribeFn);436 return supportApp.contexts.setPaths(contextName, [{ path: 'prop1', value: prop1Context }]);437 })438 .then(() => supportApp.contexts.setPaths(contextName, [{ path: 'prop2', value: prop2Context }]))439 .then(() => supportApp.contexts.setPaths(contextName, [{ path: 'prop3', value: 'value3' }]))440 .catch((err) => done(err));441 });442 });...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful