1+ const expect = require ( 'chai' ) . expect ;
2+ const Queue = require ( './queue' ) ;
3+
4+ describe ( 'Queue' , function ( ) {
5+ let queue ;
6+
7+ beforeEach ( function ( ) {
8+ queue = new Queue ( ) ;
9+ } ) ;
10+
11+ describe ( '.add' , function ( ) {
12+ it ( 'should enqueue an element' , function ( ) {
13+ queue . add ( 1 ) ;
14+ expect ( queue . list . toString ( ) ) . to . equal ( '1' ) ;
15+ } ) ;
16+ } ) ;
17+
18+ describe ( '.isEmpty' , function ( ) {
19+ it ( 'should be true' , function ( ) {
20+ expect ( queue . isEmpty ( ) ) . to . equal ( true ) ;
21+ } ) ;
22+
23+ it ( 'should be false' , function ( ) {
24+ queue . add ( 1 ) ;
25+ expect ( queue . isEmpty ( ) ) . to . equal ( false ) ;
26+ } ) ;
27+ } ) ;
28+
29+ describe ( '.remove' , function ( ) {
30+ it ( 'should nothing' , function ( ) {
31+ expect ( queue . remove ( ) ) . to . equal ( undefined ) ;
32+ } ) ;
33+
34+ it ( 'should remove element' , function ( ) {
35+ queue . add ( 1 ) ;
36+ expect ( queue . remove ( ) ) . to . equal ( 1 ) ;
37+ } ) ;
38+
39+ it ( 'should remove element on FIFO order' , function ( ) {
40+ queue . add ( 1 ) ;
41+ queue . add ( 2 ) ;
42+ expect ( queue . remove ( ) ) . to . equal ( 1 ) ;
43+ expect ( queue . remove ( ) ) . to . equal ( 2 ) ;
44+ } ) ;
45+ } ) ;
46+
47+ describe ( '.peek' , function ( ) {
48+ it ( 'should return undefined when empty' , function ( ) {
49+ expect ( queue . peek ( ) ) . to . equal ( undefined ) ;
50+ } ) ;
51+
52+ it ( 'should return first in the queue' , function ( ) {
53+ queue . add ( 1 ) ;
54+ queue . add ( 2 ) ;
55+ expect ( queue . peek ( ) ) . to . equal ( 1 ) ;
56+ } ) ;
57+ } ) ;
58+
59+ describe ( 'remove by query' , function ( ) {
60+ let dog , cat , cat2 , dog2 ;
61+ beforeEach ( function ( ) {
62+ dog = { animal : 'dog' , name : 'Lassie' } ;
63+ dog2 = { animal : 'dog' , name : 'Snoopy' } ;
64+ cat = { animal : 'cat' , name : 'Tom' } ;
65+ cat2 = { animal : 'cat' , name : 'Garfield' } ;
66+
67+ queue . add ( dog ) ;
68+ queue . add ( dog2 ) ;
69+ queue . add ( cat ) ;
70+ queue . add ( cat2 ) ;
71+ } ) ;
72+
73+ it ( 'should remove first matching' , function ( ) {
74+ expect ( queue . removeBy ( { animal : 'cat' } ) ) . to . equal ( cat ) ;
75+ expect ( queue . list . toString ( 'name' ) ) . to . equal ( 'Lassie -> Snoopy -> Garfield' ) ;
76+ } ) ;
77+ } ) ;
78+
79+ } ) ;
0 commit comments