11import { Batch } from '@mparticle/event-models' ;
22import { expect } from 'chai' ;
33import { Dictionary } from '../../src/utils' ;
4- import { SessionStorageVault , LocalStorageVault } from '../../src/vault' ;
4+ import { SessionStorageVault , LocalStorageVault , DisabledVault } from '../../src/vault' ;
55
66const testObject : Dictionary < Dictionary < string > > = {
77 foo : { foo : 'bar' , buzz : 'bazz' } ,
@@ -419,4 +419,102 @@ describe('Vault', () => {
419419 ) ;
420420 } ) ;
421421 } ) ;
422+
423+ describe ( 'DisabledVault' , ( ) => {
424+ afterEach ( ( ) => {
425+ window . localStorage . clear ( ) ;
426+ } ) ;
427+
428+ describe ( '#store' , ( ) => {
429+ it ( 'should NOT write to localStorage' , ( ) => {
430+ const storageKey = 'test-disabled-store-empty' ;
431+ const vault = new DisabledVault < Dictionary < Dictionary < string > > > (
432+ storageKey ,
433+ ) ;
434+
435+ vault . store ( testObject ) ;
436+
437+ expect ( vault . contents ) . to . equal ( null ) ;
438+ expect ( window . localStorage . getItem ( storageKey ) ) . to . equal ( null ) ;
439+ } ) ;
440+
441+ it ( 'should NOT overwrite existing localStorage value and keep contents null' , ( ) => {
442+ const storageKey = 'test-disabled-store-existing' ;
443+ window . localStorage . setItem (
444+ storageKey ,
445+ JSON . stringify ( testObject ) ,
446+ ) ;
447+
448+ const vault = new DisabledVault < Dictionary < Dictionary < string > > > (
449+ storageKey ,
450+ ) ;
451+
452+ vault . store ( { pinky : { narf : 'poit' } } ) ;
453+
454+ expect ( vault . contents ) . to . equal ( null ) ;
455+ expect ( window . localStorage . getItem ( storageKey ) ) . to . equal (
456+ JSON . stringify ( testObject ) ,
457+ ) ;
458+ } ) ;
459+ } ) ;
460+
461+ describe ( '#retrieve' , ( ) => {
462+ it ( 'should return null when nothing is stored' , ( ) => {
463+ const storageKey = 'test-disabled-retrieve-empty' ;
464+ const vault = new DisabledVault < Dictionary < Dictionary < string > > > (
465+ storageKey ,
466+ ) ;
467+ const retrievedItem = vault . retrieve ( ) ;
468+ expect ( retrievedItem ) . to . equal ( null ) ;
469+ } ) ;
470+
471+ it ( 'should return null even if localStorage has a value' , ( ) => {
472+ const storageKey = 'test-disabled-retrieve-existing' ;
473+ window . localStorage . setItem (
474+ storageKey ,
475+ JSON . stringify ( testObject ) ,
476+ ) ;
477+
478+ const vault = new DisabledVault < Dictionary < Dictionary < string > > > (
479+ storageKey ,
480+ ) ;
481+ const retrievedItem = vault . retrieve ( ) ;
482+ expect ( retrievedItem ) . to . equal ( null ) ;
483+ expect ( window . localStorage . getItem ( storageKey ) ) . to . equal (
484+ JSON . stringify ( testObject ) ,
485+ ) ;
486+ } ) ;
487+ } ) ;
488+
489+ describe ( '#purge' , ( ) => {
490+ it ( 'should keep contents null when purging' , ( ) => {
491+ const storageKey = 'test-disabled-purge-existing' ;
492+ window . localStorage . setItem (
493+ storageKey ,
494+ JSON . stringify ( testObject ) ,
495+ ) ;
496+
497+ const vault = new DisabledVault < Dictionary < Dictionary < string > > > (
498+ storageKey ,
499+ ) ;
500+
501+ vault . purge ( ) ;
502+
503+ expect ( vault . contents ) . to . equal ( null ) ;
504+ expect ( window . localStorage . getItem ( storageKey ) ) . to . equal ( null ) ;
505+ } ) ;
506+
507+ it ( 'should keep contents null when purging an empty key' , ( ) => {
508+ const storageKey = 'test-disabled-purge-empty' ;
509+ const vault = new DisabledVault < Dictionary < Dictionary < string > > > (
510+ storageKey ,
511+ ) ;
512+
513+ vault . purge ( ) ;
514+ console . log ( window . localStorage . getItem ( storageKey ) ) ;
515+ expect ( vault . contents ) . to . equal ( null ) ;
516+ expect ( window . localStorage . getItem ( storageKey ) ) . to . equal ( null ) ;
517+ } ) ;
518+ } ) ;
519+ } ) ;
422520} ) ;
0 commit comments