1+ import { assertEquals , assertInstanceOf , assertNotEquals } from "https://deno.land/std@0.151.0/testing/asserts.ts" ;
2+ import * as mod from "../mod.ts" ;
3+
4+ // Testing setup
5+ Deno . test ( "The StoreStack should be able to be instantiated" , ( ) => {
6+ const Stores = new mod . StoreStack ( ) as unknown ;
7+
8+ assertInstanceOf ( Stores , mod . StoreStack , "Stores should be instance of StoreStack" ) ;
9+ } ) ;
10+
11+ Deno . test ( "window.stores should be of instance StoreStack when importing mod.ts" , ( ) => {
12+ assertInstanceOf ( window . stores as unknown , mod . StoreStack , "window.stores was expected to be instantiated" ) ;
13+ } ) ;
14+
15+ // Testing creating a new store
16+ Deno . test ( "A new Store can be created" , ( ) => {
17+ const state = "Test" ;
18+ const store = new mod . Store ( state ) ;
19+
20+ assertInstanceOf ( store , mod . Store , "store should be an instance of Store" ) ;
21+ assertEquals ( store . state , state , `store.state should be equal to "${ state } "` ) ;
22+ } ) ;
23+
24+ Deno . test ( "A new observer can be created" , ( ) => {
25+ class ConcreteObserver implements mod . Observer < void > {
26+ public update ( _subject : mod . Store < void > ) : void {
27+ return ;
28+ }
29+ }
30+
31+ const observer = new ConcreteObserver ( ) ;
32+
33+ assertInstanceOf ( observer , ConcreteObserver , "observer should be instance of ConcreteObserver" ) ;
34+ } ) ;
35+
36+ Deno . test ( "A store can attach and trigger an observer" , ( ) => {
37+ let ouput = "" ;
38+ const expectedResult = "Test" ;
39+
40+ class ConcreteObserver implements mod . Observer < string > {
41+ public update ( subject : mod . Store < string > ) : void {
42+ ouput = subject . state ;
43+ }
44+ }
45+
46+ const store = new mod . Store ( ouput ) ;
47+ store . attach ( new ConcreteObserver ( ) ) ;
48+
49+ assertEquals ( store . state , ouput , "The ouput should equal the state" ) ;
50+
51+ store . set ( expectedResult ) ;
52+
53+ assertEquals ( store . state , expectedResult , "The ouput should equal the expectedResult" ) ;
54+ } ) ;
55+
56+ Deno . test ( "set can reuse a previous state to modify it" , ( ) => {
57+ const ouput = 0 ;
58+ const store = new mod . Store ( ouput ) ;
59+
60+ assertEquals ( store . state , ouput , `The state should equal ${ ouput } ` ) ;
61+
62+ store . set ( ( prevState ) => prevState + 1 ) ;
63+
64+ assertEquals ( store . state , ouput + 1 , "The state should increment by 1" ) ;
65+ } ) ;
0 commit comments