import 'reflect-metadata'; import CookieStorageService from './cookie-storage.service'; const initCookieStorageService = ( document: Document, ) => { return new CookieStorageService( document, ); }; describe('get', () => { // tslint:disable-next-line test('It retrieves the expected cookie from the middle of the cookies', () => { const document = { cookie: 'somecookie=abac;' + 'cookie=valueIWant;' + 'notthecookieiwant=otherval', } as Document; const cookieStorageService = initCookieStorageService( document, ); const result = cookieStorageService.get('cookie'); expect(result).toBe('valueIWant'); }); // tslint:disable-next-line test('It retrieves the object version of the cookie if it is a stringified an object', () => { const document = { cookie: 'somecookie=abac;' + 'cookie={"testObjVal": 1};' + 'notthecookieiwant=otherval', } as Document; const cookieStorageService = initCookieStorageService( document, ); const result = cookieStorageService.get('cookie'); expect(result).toEqual({ testObjVal: 1, }); }); // tslint:disable-next-line test('It returns null if no cookie is found', () => { const document = { cookie: `somecookie=abac; cookie={"testObjVal": 1}; notthecookieiwant=otherval`, } as Document; const cookieStorageService = initCookieStorageService( document, ); const result = cookieStorageService.get('thisisnotacookie'); expect(result).toBeNull(); }); }); describe('set', () => { // tslint:disable-next-line test('It sets document.cookie to the correct value', () => { const document = { cookie: 'cookie=valueIWant', } as Document; const cookieStorageService = initCookieStorageService( document, ); cookieStorageService.set( 'cookie', 'NEWVALUE', ); expect(document.cookie).toBe( 'cookie=NEWVALUE', ); }); // tslint:disable-next-line test('It processes the value sent to a string if it is an object', () => { const document = { cookie: 'cookie=valueIWant', } as Document; const cookieStorageService = initCookieStorageService( document, ); cookieStorageService.set( 'cookie', { newValue: 1, }, ); expect(document.cookie).toBe( 'cookie={"newValue":1}', ); }); });