UNPKG

3.01 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2017 TypeFox and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { Container } from 'inversify';
18import { WindowService } from './window/window-service';
19import { MockWindowService } from './window/test/mock-window-service';
20import { LocalStorageService, StorageService } from './storage-service';
21import { expect } from 'chai';
22import { ILogger } from '../common/logger';
23import { MockLogger } from '../common/test/mock-logger';
24import * as sinon from 'sinon';
25import { MessageService, MessageClient } from '../common/';
26
27let storageService: StorageService;
28
29before(() => {
30 const testContainer = new Container();
31 testContainer.bind(ILogger).toDynamicValue(ctx => {
32 const logger = new MockLogger();
33 /* Note this is not really needed but here we could just use the
34 MockLogger since it does what we need but this is there as a demo of
35 sinon for other uses-cases. We can remove this once this technique is
36 more generally used. */
37 sinon.stub(logger, 'warn').callsFake(async () => { });
38 return logger;
39 });
40 testContainer.bind(StorageService).to(LocalStorageService).inSingletonScope();
41 testContainer.bind(WindowService).to(MockWindowService).inSingletonScope();
42 testContainer.bind(LocalStorageService).toSelf().inSingletonScope();
43
44 testContainer.bind(MessageClient).toSelf().inSingletonScope();
45 testContainer.bind(MessageService).toSelf().inSingletonScope();
46
47 storageService = testContainer.get(StorageService);
48});
49
50describe('storage-service', () => {
51
52 it('stores data', async () => {
53 storageService.setData('foo', {
54 test: 'foo'
55 });
56 expect(await storageService.getData('bar', 'bar')).equals('bar');
57 expect((await storageService.getData('foo', {
58 test: 'bar'
59 })).test).equals('foo');
60 });
61
62 it('removes data', async () => {
63 storageService.setData('foo', {
64 test: 'foo'
65 });
66 expect((await storageService.getData('foo', {
67 test: 'bar'
68 })).test).equals('foo');
69
70 storageService.setData('foo', undefined);
71 expect((await storageService.getData('foo', {
72 test: 'bar'
73 })).test).equals('bar');
74 });
75
76});