3
3
/* eslint-disable no-console */
4
4
import { recordUiEdit , stores , storePrefix } from '../src/persistence' ;
5
5
6
- const _dispatch = a => {
7
- return evt => {
8
- a . push ( evt . payload . error . message ) ;
9
- }
10
- } ;
11
-
12
6
const longString = pow => {
13
7
let s = 's' ;
14
8
for ( let i = 0 ; i < pow ; i ++ ) {
@@ -74,6 +68,14 @@ describe('storage fallbacks and equivalence', () => {
74
68
const propStr = String ( propVal ) ;
75
69
let originalConsoleErr ;
76
70
let consoleCalls ;
71
+ let dispatchCalls ;
72
+
73
+ const _dispatch = evt => {
74
+ // verify that dispatch is sending errors to the devtools,
75
+ // and record the message sent
76
+ expect ( evt . type ) . toEqual ( 'ON_ERROR' ) ;
77
+ dispatchCalls . push ( evt . payload . error . message ) ;
78
+ }
77
79
78
80
beforeEach ( ( ) => {
79
81
window . my_components = {
@@ -85,8 +87,9 @@ describe('storage fallbacks and equivalence', () => {
85
87
}
86
88
} ;
87
89
88
- originalConsoleErr = console . error ;
90
+ dispatchCalls = [ ] ;
89
91
consoleCalls = [ ] ;
92
+ originalConsoleErr = console . error ;
90
93
console . error = msg => {
91
94
consoleCalls . push ( msg ) ;
92
95
} ;
@@ -107,21 +110,17 @@ describe('storage fallbacks and equivalence', () => {
107
110
const layout = layoutA ( storeType ) ;
108
111
109
112
test ( `empty ${ storeName } works` , ( ) => {
110
- const dispatchCalls = [ ] ;
111
- store . clear ( ) ;
112
-
113
- recordUiEdit ( layout , { p1 : propVal } , _dispatch ( dispatchCalls ) ) ;
113
+ recordUiEdit ( layout , { p1 : propVal } , _dispatch ) ;
114
114
expect ( dispatchCalls ) . toEqual ( [ ] ) ;
115
115
expect ( consoleCalls ) . toEqual ( [ ] ) ;
116
116
expect ( store . getItem ( `${ storePrefix } a.p1` ) ) . toEqual ( propStr ) ;
117
117
expect ( store . getItem ( `${ storePrefix } a.p1.orig` ) ) . toEqual ( 'U' ) ;
118
118
} ) ;
119
119
120
120
test ( `${ storeName } full from persistence works with warnings` , ( ) => {
121
- const dispatchCalls = [ ] ;
122
121
fillStorage ( store , `${ storePrefix } x.x` ) ;
123
122
124
- recordUiEdit ( layout , { p1 : propVal } , _dispatch ( dispatchCalls ) ) ;
123
+ recordUiEdit ( layout , { p1 : propVal } , _dispatch ) ;
125
124
expect ( dispatchCalls ) . toEqual ( [
126
125
`${ storeName } init first try failed; clearing and retrying` ,
127
126
`${ storeName } init set/get succeeded after clearing!`
@@ -134,10 +133,9 @@ describe('storage fallbacks and equivalence', () => {
134
133
} ) ;
135
134
136
135
test ( `${ storeName } full from other stuff falls back on memory` , ( ) => {
137
- const dispatchCalls = [ ] ;
138
136
fillStorage ( store , 'not_ours' ) ;
139
137
140
- recordUiEdit ( layout , { p1 : propVal } , _dispatch ( dispatchCalls ) ) ;
138
+ recordUiEdit ( layout , { p1 : propVal } , _dispatch ) ;
141
139
expect ( dispatchCalls ) . toEqual ( [
142
140
`${ storeName } init first try failed; clearing and retrying` ,
143
141
`${ storeName } init still failed, falling back to memory`
@@ -147,6 +145,22 @@ describe('storage fallbacks and equivalence', () => {
147
145
const x = Boolean ( store . getItem ( 'not_ours' ) ) ;
148
146
expect ( x ) . toBe ( true ) ;
149
147
} ) ;
148
+
149
+ test ( `${ storeName } that fills up later on just logs an error` , ( ) => {
150
+ // Maybe not ideal long-term behavior, but this is what happens
151
+
152
+ // initialize and ensure the store is happy
153
+ recordUiEdit ( layout , { p1 : propVal } , _dispatch ) ;
154
+ expect ( dispatchCalls ) . toEqual ( [ ] ) ;
155
+ expect ( consoleCalls ) . toEqual ( [ ] ) ;
156
+
157
+ // now flood it.
158
+ recordUiEdit ( layout , { p1 : longString ( 26 ) } , _dispatch ) ;
159
+ expect ( dispatchCalls ) . toEqual ( [
160
+ `a.p1 failed to save in ${ storeName } . Persisted props may be lost.`
161
+ ] ) ;
162
+ expect ( consoleCalls ) . toEqual ( dispatchCalls ) ;
163
+ } ) ;
150
164
} ) ;
151
165
152
166
[ 'local' , 'session' , 'memory' ] . forEach ( storeType => {
@@ -155,7 +169,7 @@ describe('storage fallbacks and equivalence', () => {
155
169
156
170
test ( `${ storeType } primitives in/out match` , ( ) => {
157
171
// ensure storage is instantiated
158
- recordUiEdit ( layout , { p1 : propVal } , _dispatch ( ) ) ;
172
+ recordUiEdit ( layout , { p1 : propVal } , _dispatch ) ;
159
173
const store = stores [ storeType ] ;
160
174
[
161
175
0 , 1 , 1.1 , true , false , null , undefined , '' , 'hi' , '0' , '1'
@@ -166,7 +180,7 @@ describe('storage fallbacks and equivalence', () => {
166
180
} ) ;
167
181
168
182
test ( `${ storeType } arrays and objects in/out are clones` , ( ) => {
169
- recordUiEdit ( layout , { p1 : propVal } , _dispatch ( ) ) ;
183
+ recordUiEdit ( layout , { p1 : propVal } , _dispatch ) ;
170
184
const store = stores [ storeType ] ;
171
185
172
186
[ [ 1 , 2 , 3 ] , { a : 1 , b : 2 } ] . forEach ( val => {
0 commit comments