-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.spec.ts
103 lines (73 loc) · 3.12 KB
/
queue.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {Item} from './item';
import {ItemsQueue} from './queue';
describe('ItemsQueue', () => {
let itemsQueue: ItemsQueue;
const items: Item[] = [
{id: '2', lastViewed: '2019-07-22T09:58:25.967Z', type: 'wine'},
{id: '0', lastViewed: '2019-09-22T09:58:25.967Z', type: 'wine'},
{id: '1', lastViewed: '2019-08-22T09:58:25.967Z', type: 'wine-style'},
{id: '4', lastViewed: '2018-08-22T09:58:25.967Z', type: 'wine-style'},
{id: '3', lastViewed: '2018-09-22T09:58:25.967Z', type: 'wine'}
];
const sortedItems: Item[] = [
{id: '4', lastViewed: '2018-08-22T09:58:25.967Z', type: 'wine-style'},
{id: '3', lastViewed: '2018-09-22T09:58:25.967Z', type: 'wine'},
{id: '2', lastViewed: '2019-07-22T09:58:25.967Z', type: 'wine'},
{id: '1', lastViewed: '2019-08-22T09:58:25.967Z', type: 'wine-style'},
{id: '0', lastViewed: '2019-09-22T09:58:25.967Z', type: 'wine'}
];
describe('constructor', () => {
beforeEach(() => {
itemsQueue = new ItemsQueue(items);
});
it('should initialized properties', () => {
expect(itemsQueue.getItems()).toEqual(sortedItems);
});
});
describe('enqueue', () => {
beforeEach(() => {
itemsQueue = new ItemsQueue(items);
});
it('should add a new viewed item to the array as last', () => {
const newItem: Item = {id: 'nuovo', type: 'wine', lastViewed: '2021-09-22T09:58:25.967Z'};
itemsQueue.enqueue(newItem);
expect(itemsQueue.getItems()).toEqual([...sortedItems, newItem]);
});
});
describe('dequeue', () => {
describe('if initial array is empty', () => {
beforeEach(() => {
itemsQueue = new ItemsQueue([]);
});
it('should do nothing', () => {
const expectedViewItems: Item[] = [];
itemsQueue.dequeue();
expect(itemsQueue.getItems()).toEqual(expectedViewItems);
});
});
describe('if initial list is NOT empty', () => {
beforeEach(() => {
itemsQueue = new ItemsQueue(items);
});
it('should remove the oldest item', () => {
const expectedViewItems: Item[] = [
{id: '3', lastViewed: '2018-09-22T09:58:25.967Z', type: 'wine'},
{id: '2', lastViewed: '2019-07-22T09:58:25.967Z', type: 'wine'},
{id: '1', lastViewed: '2019-08-22T09:58:25.967Z', type: 'wine-style'},
{id: '0', lastViewed: '2019-09-22T09:58:25.967Z', type: 'wine'}
];
itemsQueue.dequeue();
expect(itemsQueue.getItems()).toEqual(expectedViewItems);
});
});
});
describe('size', () => {
beforeEach(() => {
itemsQueue = new ItemsQueue(items);
});
it('should return the size of the viewed items list', () => {
const expectedSize = items.length;
expect(itemsQueue.size()).toEqual(expectedSize);
});
});
});