66 createShardKey ,
77 type CreateShardKeyData ,
88 TEST_POLLING_INTERVAL ,
9+ unmanageNamespace ,
910} from './reducer' ;
1011import sinon from 'sinon' ;
1112import type {
@@ -155,12 +156,11 @@ describe('GlobalWritesStore Store', function () {
155156 } ) ;
156157
157158 context ( 'scenarios' , function ( ) {
158- it ( 'not managed -> sharding -> success ' , async function ( ) {
159+ it ( 'not managed -> sharding -> valid shard key ' , async function ( ) {
159160 let mockShardKey = false ;
160- const hasShardKey = Sinon . fake ( ( ) => mockShardKey ) ;
161161 // initial state === unsharded
162162 const store = createStore ( {
163- hasShardKey,
163+ hasShardKey : Sinon . fake ( ( ) => mockShardKey ) ,
164164 } ) ;
165165 await store . dispatch ( fetchClusterShardingData ( ) ) ;
166166 expect ( store . getState ( ) . status ) . to . equal ( 'UNSHARDED' ) ;
@@ -182,10 +182,9 @@ describe('GlobalWritesStore Store', function () {
182182
183183 it ( 'not managed -> sharding -> sharding error' , async function ( ) {
184184 let mockFailure = false ;
185- const hasShardingError = Sinon . fake ( ( ) => mockFailure ) ;
186185 // initial state === unsharded
187186 const store = createStore ( {
188- hasShardingError,
187+ hasShardingError : Sinon . fake ( ( ) => mockFailure ) ,
189188 } ) ;
190189 await store . dispatch ( fetchClusterShardingData ( ) ) ;
191190 expect ( store . getState ( ) . status ) . to . equal ( 'UNSHARDED' ) ;
@@ -205,7 +204,7 @@ describe('GlobalWritesStore Store', function () {
205204 } ) ;
206205 } ) ;
207206
208- it ( 'not managed -> failed sharding attempt ' , async function ( ) {
207+ it ( 'not managed -> not managed ( failed sharding request) ' , async function ( ) {
209208 const store = createStore ( {
210209 failsOnShardingRequest : ( ) => true ,
211210 } ) ;
@@ -219,19 +218,83 @@ describe('GlobalWritesStore Store', function () {
219218 expect ( store . getState ( ) . status ) . to . equal ( 'UNSHARDED' ) ;
220219 } ) ;
221220
222- it ( 'when the namespace is managed and has a valid shard key' , async function ( ) {
221+ it ( 'sharding -> valid shard key' , async function ( ) {
222+ let mockShardKey = false ;
223+ // initial state === sharding
224+ const store = createStore ( {
225+ isNamespaceManaged : ( ) => true ,
226+ hasShardKey : Sinon . fake ( ( ) => mockShardKey ) ,
227+ } ) ;
228+ await store . dispatch ( fetchClusterShardingData ( ) ) ;
229+ await waitFor ( ( ) => {
230+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARDING' ) ;
231+ expect ( store . getState ( ) . managedNamespace ) . to . equal ( managedNamespace ) ;
232+ } ) ;
233+
234+ // sharding ends with a shardKey
235+ mockShardKey = true ;
236+ await wait ( TEST_POLLING_INTERVAL ) ;
237+ await waitFor ( ( ) => {
238+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ;
239+ } ) ;
240+ } ) ;
241+
242+ it ( 'valid shard key' , async function ( ) {
243+ const store = createStore ( {
244+ isNamespaceManaged : ( ) => true ,
245+ hasShardKey : ( ) => true ,
246+ } ) ;
247+ await store . dispatch ( fetchClusterShardingData ( ) ) ;
248+ await waitFor ( ( ) => {
249+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ;
250+ expect ( store . getState ( ) . managedNamespace ) . to . equal ( managedNamespace ) ;
251+ } ) ;
252+ } ) ;
253+
254+ it ( 'valid shard key -> not managed' , async function ( ) {
255+ const store = createStore ( {
256+ isNamespaceManaged : ( ) => true ,
257+ hasShardKey : ( ) => true ,
258+ } ) ;
259+
260+ // initial state === shard key correct
261+ await store . dispatch ( fetchClusterShardingData ( ) ) ;
262+ await waitFor ( ( ) => {
263+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ;
264+ expect ( store . getState ( ) . managedNamespace ) . to . equal ( managedNamespace ) ;
265+ } ) ;
266+
267+ // user asks to unmanage
268+ const promise = store . dispatch ( unmanageNamespace ( ) ) ;
269+ expect ( store . getState ( ) . status ) . to . equal ( 'UNMANAGING_NAMESPACE' ) ;
270+ await promise ;
271+ expect ( store . getState ( ) . status ) . to . equal ( 'UNSHARDED' ) ;
272+ } ) ;
273+
274+ it ( 'valid shard key -> valid shard key (failed unmanage attempt)' , async function ( ) {
275+ let mockFailure = false ;
223276 const store = createStore ( {
224277 isNamespaceManaged : ( ) => true ,
225278 hasShardKey : ( ) => true ,
279+ failsOnShardingRequest : Sinon . fake ( ( ) => mockFailure ) ,
226280 } ) ;
281+
282+ // initial state === shard key correct
227283 await store . dispatch ( fetchClusterShardingData ( ) ) ;
228284 await waitFor ( ( ) => {
229285 expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ;
230286 expect ( store . getState ( ) . managedNamespace ) . to . equal ( managedNamespace ) ;
231287 } ) ;
288+
289+ // user asks to unmanage
290+ mockFailure = true ;
291+ const promise = store . dispatch ( unmanageNamespace ( ) ) ;
292+ expect ( store . getState ( ) . status ) . to . equal ( 'UNMANAGING_NAMESPACE' ) ;
293+ await promise ;
294+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ;
232295 } ) ;
233296
234- it ( 'when the namespace is managed but has a sharding error' , async function ( ) {
297+ it ( 'sharding error' , async function ( ) {
235298 const store = createStore ( {
236299 isNamespaceManaged : ( ) => true ,
237300 hasShardingError : ( ) => true ,
0 commit comments