Skip to content

Commit e104aea

Browse files
committed
tests refactoring: Storages
1 parent 49d3733 commit e104aea

File tree

2 files changed

+267
-108
lines changed

2 files changed

+267
-108
lines changed

test/unit-tests/storages/ArrayStorage.spec.ts

Lines changed: 132 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,117 +10,195 @@ class ConcreteStorage extends ArrayStorage <string> {
1010
}
1111
}
1212

13+
/**
14+
* @type {IStorage<any>}
15+
*/
16+
const getStorageInstance = (): IStorage <any> => {
17+
const storage: IStorage<any> = new ConcreteStorage();
18+
19+
storage.initialize();
20+
21+
return storage;
22+
};
23+
1324
describe('ArrayStorage', () => {
25+
const storageKey: number = 0;
26+
const storageValue: string = 'foo';
27+
28+
let storage: IStorage <any>;
29+
1430
describe('initialize (...args: any[]): void', () => {
15-
it('should throws an error when storage isn\'t initialized', () => {
16-
const storage: IStorage <string> = new ConcreteStorage();
31+
const expectedError: ErrorConstructor = Error;
32+
33+
let testFunc: () => void;
1734

18-
assert.throws(() => storage.set(0, 'foo'), Error);
35+
before(() => {
36+
storage = new ConcreteStorage();
37+
testFunc = () => storage.set(storageKey, storageValue);
38+
});
39+
40+
it('should throws an error when storage isn\'t initialized', () => {
41+
assert.throws(testFunc, expectedError);
1942
});
2043
});
2144

2245
describe('getStorage (): T[]', () => {
23-
it('should returns storage', () => {
24-
const storage: IStorage <string> = new ConcreteStorage();
46+
const expectedInstanceOf: ArrayConstructor = Array;
47+
48+
let arrayStorage: string[];
49+
50+
before(() => {
51+
storage = getStorageInstance();
2552

26-
storage.initialize();
53+
arrayStorage = storage.getStorage();
54+
});
2755

28-
assert.instanceOf(storage.getStorage(), Array);
56+
it('should returns storage', () => {
57+
assert.instanceOf(arrayStorage, expectedInstanceOf);
2958
});
3059
});
3160

3261
describe('get (key: number): T', () => {
33-
it('should returns value from storage by key', () => {
34-
const storage: IStorage <string> = new ConcreteStorage();
62+
describe('variant #1: value exist', () => {
63+
const expectedValue: string = storageValue;
3564

36-
storage.initialize();
37-
storage.set(0, 'foo');
65+
let value: string;
3866

39-
assert.equal(storage.get(0), 'foo');
67+
before(() => {
68+
storage = getStorageInstance();
69+
storage.set(storageKey, storageValue);
70+
71+
value = storage.get(storageKey);
72+
});
73+
74+
it('should returns value from storage by key', () => {
75+
assert.equal(value, expectedValue);
76+
});
4077
});
4178

42-
it('should throw an error if value isn\'t exist', () => {
43-
const storage: IStorage <string> = new ConcreteStorage();
79+
describe('variant #2: value isn\'t exist', () => {
80+
const expectedError: ErrorConstructor = Error;
81+
82+
let testFunc: () => void;
4483

45-
storage.initialize();
84+
before(() => {
85+
storage = getStorageInstance();
4686

47-
assert.throws(() => storage.get(0), Error);
87+
testFunc = () => storage.get(storageKey);
88+
});
89+
90+
it('should throw an error', () => {
91+
assert.throws(testFunc, expectedError);
92+
});
4893
});
4994
});
5095

5196
describe('getLength (): number', () => {
52-
it('should returns length of storage', () => {
53-
const storage: IStorage <string> = new ConcreteStorage();
97+
const expectedStorageLength: number = 1;
98+
99+
let storageLength: number;
54100

55-
storage.initialize();
56-
storage.set(0, 'foo');
101+
before(() => {
102+
storage = getStorageInstance();
103+
storage.set(storageKey, storageValue);
57104

58-
assert.equal(storage.getLength(), 1);
105+
storageLength = storage.getLength();
106+
});
107+
108+
it('should returns length of storage', () => {
109+
assert.equal(storageLength, expectedStorageLength);
59110
});
60111
});
61112

62113
describe('getKeyOf (value: T): number | null', () => {
63-
it('should returns key of string value', () => {
64-
const storage: IStorage <string> = new ConcreteStorage();
114+
let key: string | number | null;
65115

66-
storage.initialize();
67-
storage.set(0, 'foo');
116+
describe('variant #1', () => {
117+
before(() => {
118+
storage = getStorageInstance();
119+
storage.set(storageKey, storageValue);
120+
121+
key = storage.getKeyOf(storageValue);
122+
});
68123

69-
assert.equal(storage.getKeyOf('foo'), 0);
124+
it('should returns key of string value', () => {
125+
assert.equal(key, storageKey);
126+
});
70127
});
71128

72-
it('should returns key of object if objects in `set` and `get` are two linked objects', () => {
73-
const storage: IStorage <Object> = new ConcreteStorage();
129+
describe('variant #2', () => {
74130
const object: Object = {
75131
foo: 'bar'
76132
};
77133

78-
storage.initialize();
79-
storage.set(0, object);
134+
before(() => {
135+
storage = getStorageInstance();
136+
storage.set(storageKey, object);
80137

81-
assert.equal(storage.getKeyOf(object), 0);
82-
});
138+
key = storage.getKeyOf(object);
139+
});
83140

84-
it('should return `null` if objects in `set` and `get` are two equal objects', () => {
85-
const storage: IStorage <Object> = new ConcreteStorage();
141+
it('should returns key of object if objects in `set` and `get` are two same objects', () => {
142+
assert.equal(key, storageKey);
143+
});
144+
});
86145

87-
storage.initialize();
88-
storage.set(0, {
146+
describe('variant #3', () => {
147+
const expectedKey: null = null;
148+
const object: Object = {
89149
foo: 'bar'
150+
};
151+
152+
before(() => {
153+
storage = getStorageInstance();
154+
storage.set(storageKey, object);
155+
156+
key = storage.getKeyOf({...object});
90157
});
91158

92-
assert.equal(storage.getKeyOf({
93-
foo: 'bar'
94-
}), null);
159+
it('should return `null` if objects in `set` and `get` are two different objects', () => {
160+
assert.equal(key, expectedKey);
161+
});
95162
});
96163
});
97164

98165
describe('set (key: number, value: T): void', () => {
99-
it('should set value to the storage', () => {
100-
const storage: IStorage <string> = new ConcreteStorage();
166+
let value: string;
101167

102-
storage.initialize();
103-
storage.set(0, 'foo');
168+
before(() => {
169+
storage = getStorageInstance();
170+
storage.set(storageKey, storageValue);
104171

105-
assert.equal(storage.get(0), 'foo');
106-
assert.equal(storage.getLength(), 1);
172+
value = storage.get(storageKey);
173+
});
174+
175+
it('should set value to the storage', () => {
176+
assert.equal(value, storageValue);
107177
});
108178
});
109179

110180
describe('mergeWith (storage: this, mergeId: boolean = false): void', () => {
111-
it('should merge two storages', () => {
112-
const storage1: IStorage <string> = new ConcreteStorage();
113-
const storage2: IStorage <string> = new ConcreteStorage();
181+
const secondStorageKey: number = 1;
182+
const secondStorageValue: string = 'bar';
183+
184+
const expectedArray: string[] = [storageValue, secondStorageValue];
185+
186+
let array: string[];
114187

115-
storage1.initialize();
116-
storage1.set(0, 'foo');
188+
before(() => {
189+
storage = getStorageInstance();
190+
storage.set(storageKey, storageValue);
117191

118-
storage2.initialize();
119-
storage2.set(1, 'bar');
192+
const secondStorage: IStorage <string> = getStorageInstance();
193+
secondStorage.set(secondStorageKey, secondStorageValue);
120194

121-
storage1.mergeWith(storage2, false);
195+
storage.mergeWith(secondStorage, false);
122196

123-
assert.deepEqual(storage1.getStorage(), ['foo', 'bar']);
197+
array = storage.getStorage();
198+
});
199+
200+
it('should merge two storages', () => {
201+
assert.deepEqual(array, expectedArray);
124202
});
125203
});
126204
});

0 commit comments

Comments
 (0)