"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const StorageProvider_1 = require("../../../../src/providers/storage/StorageProvider");
describe('Session Storage Provider', () => {
    let savedLocalStorage;
    beforeEach(() => {
        savedLocalStorage = window.localStorage;
        localStorage.clear();
        sessionStorage.clear();
    });
    afterEach(() => {
        Object.defineProperty(window, 'localStorage', {
            value: savedLocalStorage,
        });
    });
    describe('Writing To Storage', () => {
        it('writes a number successfully to local storage', () => {
            const provider = new StorageProvider_1.StorageProvider();
            provider.write('example', 2);
            expect(localStorage.getItem('roomstay-example')).toBe(JSON.stringify(2));
        });
        it('writes a string successfully to local storage', () => {
            const provider = new StorageProvider_1.StorageProvider();
            provider.write('example', 'simple string');
            expect(localStorage.getItem('roomstay-example')).toBe(JSON.stringify('simple string'));
        });
        it('writes an object successfully to local storage', () => {
            const provider = new StorageProvider_1.StorageProvider();
            provider.write('example', { test: 1, two: 2, three: 'four' });
            expect(localStorage.getItem('roomstay-example')).toBe(JSON.stringify({
                test: 1,
                two: 2,
                three: 'four',
            }));
        });
        it('gracefully fails if user has specifically denied local storage', () => {
            var localStorageMock = (function () {
                return {
                    setItem: function (key, value) {
                        throw new Error('Forced Exception');
                    },
                };
            })();
            const provider = new StorageProvider_1.StorageProvider(localStorageMock);
            return expect(provider.write('item', 'value')).resolves.toBeFalsy();
        });
    });
    describe('Reading from storage', () => {
        it('reads a string properly', () => {
            const provider = new StorageProvider_1.StorageProvider();
            provider.write('test', 'this will be fun');
            return expect(provider.read('test')).resolves.toBe('this will be fun');
        });
        it('gracefully fails if someone / something injected our local storage', () => {
            const provider = new StorageProvider_1.StorageProvider();
            localStorage.setItem('roomstay-test', '{addqwdqdqe}');
            return expect(provider.read('test')).resolves.toBe(null);
        });
        it('gracefully fails if we incur an error while reading from storage', () => {
            var localStorageMock = (function () {
                return {
                    getItem: function (key) {
                        throw new Error('Forced Exception');
                    },
                    setItem: window.localStorage.setItem,
                };
            })();
            const provider = new StorageProvider_1.StorageProvider(localStorageMock);
            return expect(provider.read('test')).resolves.toBe(null);
        });
        it('lists all the keys of the objects stored in storage', () => {
            localStorage.setItem('roomstay-thing', 'is cool');
            localStorage.setItem('roomstay-yoke', 'is cool');
            localStorage.setItem('roomstay-engine', 'is cool');
            localStorage.setItem('roomstay-wonder', 'is cool');
            const storage = new StorageProvider_1.StorageProvider();
            expect(storage.getAllKeys().length).toBe(4);
        });
        it('lists all the keys of the objects stored in storage', () => {
            localStorage.setItem('roomstay-thing', 'is cool');
            localStorage.setItem('roomstay-yoke', 'is cool');
            localStorage.setItem('engine', 'is cool');
            localStorage.setItem('wonder', 'is cool');
            const storage = new StorageProvider_1.StorageProvider();
            expect(storage.getAllKeys().length).toBe(2);
        });
        it('list all the keys matching a filter', () => {
            localStorage.setItem('roomstay-thing', 'is cool');
            localStorage.setItem('roomstay-session-yoke', 'is cool');
            localStorage.setItem('engine', 'is cool');
            localStorage.setItem('wonder', 'is cool');
            const storage = new StorageProvider_1.StorageProvider();
            expect(storage.getAllKeys((name) => name.startsWith('roomstay-session')).length).toBe(1);
        });
        it('reads multiple list of objects based on an array of keys provided', () => __awaiter(void 0, void 0, void 0, function* () {
            const value = JSON.stringify('this is cool');
            localStorage.setItem('roomstay-thing', value);
            localStorage.setItem('roomstay-session-yoke', value);
            localStorage.setItem('engine', value);
            const storage = new StorageProvider_1.StorageProvider();
            const values = yield storage.readAll(['thing', 'session-yoke']);
            expect(values.length).toBe(2);
        }));
    });
});
//# sourceMappingURL=LocalStorageProvider.test.js.map