import rewiremock from 'rewiremock';
import '../../../../mocks/electron';

import { expect } from 'chai';
import 'mocha';
import { container } from '../../core/container';
import { IStore } from './i-store';

describe('Store Service', function () {
  this.timeout(10000);

  before(() => {
    rewiremock.enable();
  });

  after(() => {
    rewiremock.disable();
  });

  it('loads saved data', () => {
    const store: IStore = container.get('store');
    const testData = {
      something: 'gaga',
      somethingElse: 0,
      deepObject: {
        somethingAsWell: true,
        somethingNotInJson: undefined,
        someArray: [
          'hui',
          {
            g: 'h',
          },
        ],
      },
    };
    const expectedJson = JSON.stringify(testData);
    return store
      .save(StoreKey.COOKIES, testData)
      .then(() => store.load(StoreKey.COOKIES))
      .then((data) => {
        expect(JSON.stringify(data)).to.equal(expectedJson, 'store does not save and load data correctly');
        return store.load(StoreKey.COOKIES);
      })
      .then((data) => {
        expect(JSON.stringify(data)).to.equal(expectedJson, 'store does not load data correctly when loaded twice');
      });
  });
});
