/**
 * `googlesitekit/modules` datastore: changes tests.
 *
 * Site Kit by Google, Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Internal dependencies
 */
import Data from 'googlesitekit-data';
import Modules from 'googlesitekit-modules';
import { STORE_NAME } from './constants';
import { createTestRegistry } from '../../../../../tests/js/utils';

describe( 'core/modules store changes', () => {
	let registry;
	const slug = 'test-module';
	const nonExistentModuleSlug = 'not-module';
	const moduleStoreName = `modules/${ slug }`;
	let submittingChanges = false;

	beforeAll( () => {
	} );

	beforeEach( () => {
		const storeDefinition = Modules.createModuleStore( moduleStoreName );
		const submitChanges = jest.fn();
		const canSubmitChanges = jest.fn();
		const isDoingSubmitChanges = () => {
			return submittingChanges;
		};

		registry = createTestRegistry();
		registry.registerStore( moduleStoreName, Data.combineStores(
			storeDefinition,
			{
				actions: {
					canSubmitChanges,
				},
				selectors: {
					submitChanges,
					isDoingSubmitChanges,
				},
			}
		) );
	} );

	afterAll( () => {
	} );

	afterEach( () => {
	} );

	describe( 'selectors', () => {
		describe( 'isDoingSubmitChanges', () => {
			it( 'is submitting changes', async () => {
				expect( registry.select( STORE_NAME ).isDoingSubmitChanges( nonExistentModuleSlug ) ).toBe( false );

				// @TODO  select( `modules/${ slug }` ) returns false when called via the test
				expect( registry.select( STORE_NAME ).isDoingSubmitChanges( moduleStoreName ) ).toBe( false );
				submittingChanges = true;
				expect( registry.select( STORE_NAME ).isDoingSubmitChanges( moduleStoreName ) ).toBe( true );
			} );
		} );

		describe( 'submits changes', () => {
			it( 'can submit changes', () => {
				expect( registry.select( STORE_NAME ).canSubmitChanges( nonExistentModuleSlug ) ).toBe( false );

				// @TODO  select( `modules/${ slug }` ) returns false when called via the test
				expect( registry.select( STORE_NAME ).canSubmitChanges( moduleStoreName ) ).toBe( true );
			} );

			it( 'does submit changes', async () => {
				const expectedError = { error: `'modules/${ nonExistentModuleSlug }' does not have a submitChanges() action.` };

				expect( await registry.dispatch( STORE_NAME ).submitChanges( nonExistentModuleSlug ) ).toEqual( expectedError );
				expect( await registry.dispatch( STORE_NAME ).submitChanges( slug ) ).toBeTruthy();
			} );
		} );
	} );
} );

@@ -1077,16 +1077,53 @@
 lug %7D%60;%0A
+%09let moduleCanSubmitChanges = false;%0A
 %09let sub
@@ -1146,16 +1146,16 @@
  false;%0A
-
 %0A%09before
@@ -1325,33 +1325,52 @@
 itChanges = 
-jest.fn()
+() =%3E moduleCanSubmitChanges
 ;%0A%09%09const is
@@ -1576,20 +1576,17 @@
  %7B%0A%09%09%09%09%09
-canS
+s
 ubmitCha
@@ -1612,33 +1612,36 @@
 lectors: %7B%0A%09%09%09%09%09
-s
+canS
 ubmitChanges,%0A%09%09
@@ -2434,34 +2434,17 @@
 hanges( 
-nonExistentModuleS
+s
 lug ) ).
@@ -2462,91 +2462,125 @@
  );%0A
-%0A
 %09%09%09%09
-// @TODO  select( %60modules/$%7B slug %7D%60 ) returns false when called via the test
+moduleCanSubmitChanges = true;%0A%09%09%09%09expect( registry.select( STORE_NAME ).canSubmitChanges( slug ) ).toBe( true );
 %0A%09%09%09
@@ -2632,39 +2632,45 @@
 mitChanges( 
-moduleStoreName
+nonExistentModuleSlug
  ) ).toBe( t
@@ -2660,35 +2660,36 @@
 eSlug ) ).toBe( 
-tru
+fals
 e );%0A%09%09%09%7D );%0A%0A%09%09

Fix mixup in actions/selectors.