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 with default path if not specified', () => { (Date.now as any) = jest.fn(() => new Date(2000, 1, 1)); const document = { cookie: 'cookie=valueIWant', } as Document; const cookieStorageService = initCookieStorageService( document, ); cookieStorageService.set( 'cookie', 'NEWVALUE', ); expect(document.cookie).toBe( 'cookie=NEWVALUE;path=/;SameSite=None; Secure', ); }); // tslint:disable-next-line test('It processes the value sent to a string if it is an object, with default path if not specified', () => { (Date.now as any) = jest.fn(() => new Date(2000, 1, 1)); const document = { cookie: 'cookie=valueIWant', } as Document; const cookieStorageService = initCookieStorageService( document, ); cookieStorageService.set( 'cookie', { newValue: 1, }, ); expect(document.cookie).toBe( 'cookie={"newValue":1};path=/;SameSite=None; Secure', ); }); // tslint:disable-next-line test('Sets the expiry to the value set as an argument if supplied, with default path if not specified', () => { const document = { cookie: 'cookie=valueIWant', } as Document; const cookieStorageService = initCookieStorageService( document, ); const date = new Date(Date.UTC(2005, 5, 15)); cookieStorageService.set( 'cookie', 'NEWVALUE', date, ); expect(document.cookie).toBe( 'cookie=NEWVALUE;expires=Wed, 15 Jun 2005 00:00:00 GMT;path=/;SameSite=None; Secure', ); }); // tslint:disable-next-line test('Sets the domain to the value set as an argument if supplied, with default path if not specified', () => { const document = { cookie: 'cookie=valueIWant', } as Document; const cookieStorageService = initCookieStorageService( document, ); cookieStorageService.set( 'cookie', 'NEWVALUE', undefined, 'testDomain', ); expect(document.cookie).toBe( 'cookie=NEWVALUE;domain=testDomain;path=/;SameSite=None; Secure', ); }); test('Sets the path to the value set as an argument if supplied', () => { const document = { cookie: 'cookie=valueIWant', } as Document; const cookieStorageService = initCookieStorageService( document, ); cookieStorageService.set( 'cookie', 'NEWVALUE', undefined, undefined, 'testPath', ); expect(document.cookie).toBe( 'cookie=NEWVALUE;path=testPath;SameSite=None; Secure', ); }); });