11import HistoryEvent from '../../src/history/HistoryEvent' ;
2- import HistoryStack from '../../src/history/HistoryStack' ;
2+ import HistoryStack , { computedParam } from '../../src/history/HistoryStack' ;
33import CategoricalScale from '../../src/scales/CategoricalScale' ;
4+ import DataContainer from '../../src/data/DataContainer' ;
45
56let getScale ;
7+ let getData ;
68let sampleScale ;
9+ let sampleData ;
710beforeEach ( ( ) => {
811 sampleScale = new CategoricalScale ( "sample_id" , "Samples" , [ "S1" , "S2" , "S3" , "S4" , "S5" , "S6" ] ) ;
912 getScale = ( ) => sampleScale ;
13+
14+ sampleData = [
15+ {
16+ "sample_id" : "S1" ,
17+ "age" : 3
18+ } ,
19+ {
20+ "sample_id" : "S2" ,
21+ "age" : 2
22+ } ,
23+ {
24+ "sample_id" : "S3" ,
25+ "age" : 10
26+ } ,
27+ {
28+ "sample_id" : "S4" ,
29+ "age" : 1
30+ } ,
31+ {
32+ "sample_id" : "S5" ,
33+ "age" : 5
34+ }
35+ ] ;
36+
37+ let sampleDataContainer = new DataContainer ( "sample_data" , "Sample Data" , sampleData ) ;
38+
39+ getData = ( ) => sampleDataContainer ;
1040} ) ;
1141
1242test ( 'able to create a HistoryStack' , ( ) => {
@@ -81,4 +111,61 @@ test('able to execute event, go back and go forward', () => {
81111 stack . goForward ( ) ;
82112 expect ( sampleScale . domainFiltered . length ) . toBe ( 4 ) ;
83113
114+ } ) ;
115+
116+ test ( 'able to get computedParam JSON object' , ( ) => {
117+ let cp = computedParam ( "getData" , [ "my_data" ] )
118+
119+ expect ( cp ) . toHaveProperty ( "$vdp_val_from_getter" ) ;
120+ expect ( cp ) . toHaveProperty ( "getterFunction" ) ;
121+ expect ( cp ) . toHaveProperty ( "getterParams" ) ;
122+ } ) ;
123+
124+ test ( 'able to execute event with computed parameter' , ( ) => {
125+ let stack = new HistoryStack ( getScale , getData ) ;
126+
127+ let e0 = new HistoryEvent ( HistoryEvent . types . SCALE , "sample_id" , "reset" ) ;
128+ stack . push ( e0 , true ) ;
129+
130+ expect ( sampleScale . domain ) . toEqual ( sampleScale . domainFiltered ) ;
131+ expect ( sampleScale . domainFiltered ) . toEqual ( [ "S1" , "S2" , "S3" , "S4" , "S5" , "S6" ] ) ;
132+
133+ // Sort by age ascending, store in stack
134+ sampleScale . sort ( getData ( ) , "age" , true ) ;
135+ let e1 = new HistoryEvent ( HistoryEvent . types . SCALE , "sample_id" , "sort" , [ computedParam ( "getData" , [ ] ) , "age" , true ] ) ;
136+ stack . push ( e1 ) ;
137+
138+ expect ( sampleScale . domain ) . toEqual ( sampleScale . domainFiltered ) ;
139+ expect ( sampleScale . domainFiltered ) . toEqual ( [ "S6" , "S4" , "S2" , "S1" , "S5" , "S3" ] ) ;
140+
141+ // Sort by age descending, store in stack
142+ sampleScale . sort ( getData ( ) , "age" , false ) ;
143+ let e2 = new HistoryEvent ( HistoryEvent . types . SCALE , "sample_id" , "sort" , [ computedParam ( "getData" , [ ] ) , "age" , false ] ) ;
144+ stack . push ( e2 ) ;
145+
146+ expect ( sampleScale . domain ) . toEqual ( sampleScale . domainFiltered ) ;
147+ expect ( sampleScale . domainFiltered ) . toEqual ( [ "S3" , "S5" , "S1" , "S2" , "S4" , "S6" ] ) ;
148+
149+ // Go back and forward, check resulting domain orderings
150+
151+ // Back to ascending
152+ expect ( stack . canGoBack ( ) ) . toBe ( true ) ;
153+ stack . goBack ( ) ;
154+ expect ( sampleScale . domain ) . toEqual ( sampleScale . domainFiltered ) ;
155+ expect ( sampleScale . domainFiltered ) . toEqual ( [ "S6" , "S4" , "S2" , "S1" , "S5" , "S3" ] ) ;
156+
157+ // Forward to descending
158+ expect ( stack . canGoForward ( ) ) . toBe ( true ) ;
159+ stack . goForward ( ) ;
160+ expect ( sampleScale . domain ) . toEqual ( sampleScale . domainFiltered ) ;
161+ expect ( sampleScale . domainFiltered ) . toEqual ( [ "S3" , "S5" , "S1" , "S2" , "S4" , "S6" ] ) ;
162+
163+ // Back twice to original "reset" ordering
164+ expect ( stack . canGoBack ( ) ) . toBe ( true ) ;
165+ stack . goBack ( ) ;
166+ expect ( stack . canGoBack ( ) ) . toBe ( true ) ;
167+ stack . goBack ( ) ;
168+ expect ( sampleScale . domain ) . toEqual ( sampleScale . domainFiltered ) ;
169+ expect ( sampleScale . domainFiltered ) . toEqual ( [ "S1" , "S2" , "S3" , "S4" , "S5" , "S6" ] ) ;
170+
84171} ) ;
0 commit comments