diff --git a/src/constants/store-actions.js b/src/constants/store-actions.js index ebffdebde..392e7b59e 100644 --- a/src/constants/store-actions.js +++ b/src/constants/store-actions.js @@ -54,6 +54,7 @@ const storeActions = { RESET_DAMAGE_STATE_AND_DEPENDENCIES: "resetDamageAndDependencies", RESET_REGISTRATION_STATE_AND_DEPENDENCIES: "resetRegistrationAndDependencies", RESET_PARTS_STATE_AND_DEPENDENCIES: "resetPartsAndDependencies", + RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES: "resetServiceLocationAndDependencies", RESET_STATE: "resetState", // SAVE COMPONENT STATE diff --git a/src/store/index.js b/src/store/index.js index eac829543..7ad5c4457 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -726,6 +726,15 @@ export const actions = { resetPartsAndDependencies(context) { context.commit(storeMutations.RESET_GLASS_PARTS_STATE); context.commit(storeMutations.UPDATE_SUPPORTING_ITEMS, null); + + context.dispatch(storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES); + }, + + resetServiceLocationAndDependencies(context) { + context.commit(storeMutations.RESET_SERVICE_LOCATION_APPOINTMENT_TYPE); + context.commit(storeMutations.RESET_SERVICE_LOCATION_PROVIDER); + + context.commit(storeMutations.RESET_SCHEDULE); }, resetState(context) { @@ -1777,8 +1786,7 @@ export const actions = { saveSupportingItems(context, supportingItems) { if (!deepEqual(supportingItems, context.state.order.lineItems.supportingItems)) { - context.commit(storeMutations.RESET_SERVICE_LOCATION_APPOINTMENT_TYPE); - context.commit(storeMutations.RESET_SERVICE_LOCATION_PROVIDER); + context.dispatch(storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES); } context.commit(storeMutations.UPDATE_SUPPORTING_ITEMS, supportingItems); @@ -1854,8 +1862,8 @@ export const actions = { context.state.order.serviceLocation && serviceZipCodeInfo.zipCode !== context.state.order.serviceLocation.zipCode ) { - context.commit(storeMutations.RESET_SERVICE_LOCATION_APPOINTMENT_TYPE); - context.commit(storeMutations.RESET_SERVICE_LOCATION_PROVIDER); + context.dispatch(storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES); + context.commit(storeMutations.RESET_SERVICE_LOCATION_MOBILE_ADDRESS); } @@ -1863,6 +1871,20 @@ export const actions = { }, saveServiceLocation(context, serviceLocationInfo) { + if (context.state.order.serviceLocation) { + if ( + serviceLocationInfo.zipCode !== context.state.order.serviceLocation.zipCode || + !deepEqual( + serviceLocationInfo.provider, + context.state.order.serviceLocation.provider + ) || + serviceLocationInfo.appointmentType !== + context.state.order.serviceLocation.appointmentType + ) { + context.commit(storeMutations.RESET_SCHEDULE); + } + } + context.commit(storeMutations.UPDATE_SERVICE_LOCATION, serviceLocationInfo); }, @@ -1885,8 +1907,7 @@ export const actions = { saveGlassParts(context, parts) { if (!deepEqual(parts, context.state.order.lineItems.glassParts)) { - context.commit(storeMutations.RESET_SERVICE_LOCATION_APPOINTMENT_TYPE); - context.commit(storeMutations.RESET_SERVICE_LOCATION_PROVIDER); + context.dispatch(storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES); } context.commit(storeMutations.UPDATE_GLASS_PARTS, parts); diff --git a/src/store/store.spec.js b/src/store/store.spec.js index 262cb263a..847701e67 100644 --- a/src/store/store.spec.js +++ b/src/store/store.spec.js @@ -575,13 +575,16 @@ describe("Actions", () => { // Arrange const context = state; const commit = jest.fn(); + const dispatch = jest.fn(); context.commit = commit; + context.dispatch = dispatch; // Act await actions.resetPartsAndDependencies(context); expect(commit).toBeCalledWith(storeMutations.RESET_GLASS_PARTS_STATE); + expect(dispatch).toBeCalledWith(storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES); }); it("resetState action", async () => { @@ -933,6 +936,90 @@ describe("Actions", () => { ); }); + it("saveServiceZipCodeInfo, should call mutation and save zip code to state", () => { + // Arrange + const context = state; + const commit = jest.fn(); + context.commit = commit; + + const serviceZipCodeInfo = { + zipCode: "43212", + }; + + // Act + actions.saveServiceLocation(context, serviceZipCodeInfo); + + // Assert + expect(commit).toBeCalledWith(storeMutations.UPDATE_SERVICE_LOCATION, serviceZipCodeInfo); + expect(state.order.serviceLocation.zipCode).toEqual("43212"); + }); + + it("saveServiceZipCodeInfo, should reset if zip code is different", () => { + // Arrange + const context = { + state: { + order: { + serviceLocation: { + zipCode: "43212", + }, + }, + }, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + const serviceZipCodeInfo = { + zipCode: "43202", + }; + + // Act + actions.saveServiceZipCodeInfo(context, serviceZipCodeInfo); + + // Assert + expect(dispatch).toHaveBeenCalledWith( + storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES + ); + expect(commit).toHaveBeenCalledWith(storeMutations.RESET_SERVICE_LOCATION_MOBILE_ADDRESS); + }); + + it("saveServiceZipCodeInfo, should not reset if zip code is the same", () => { + // Arrange + const context = { + state: { + order: { + serviceLocation: { + zipCode: "43212", + }, + }, + }, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + const serviceZipCodeInfo = { + zipCode: "43212", + }; + + // Act + actions.saveServiceZipCodeInfo(context, serviceZipCodeInfo); + + // Assert + expect(dispatch).not.toHaveBeenCalledWith( + storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES + ); + expect(commit).not.toHaveBeenCalledWith( + storeMutations.RESET_SERVICE_LOCATION_MOBILE_ADDRESS + ); + }); + it("saveServiceLocation, should call mutation and save service address to state", () => { // Arrange const context = state; @@ -959,6 +1046,242 @@ describe("Actions", () => { expect(state.order.serviceLocation.zipCodeCtu).toEqual("01820"); }); + it("saveServiceLocation, should reset if zipcode is different", () => { + // Arrange + const context = { + state: { + order: { + serviceLocation: { + address: "123 Test Lane", + city: "Columbus", + zipCode: "43212", + state: "OH", + zipCodeCtu: "01820", + appointmentType: "Mobile", + isVehicleProtected: true, + provider: { + providerNumber: "11111", + address: { + streetAddress: "123 Test Lane", + city: "Columbus", + state: "OH", + zip: "43212", + }, + }, + }, + }, + }, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + const serviceLocation = { + address: "123 Test Lane", + city: "Columbus", + zipCode: "43202", + state: "OH", + zipCodeCtu: "01820", + appointmentType: "Mobile", + isVehicleProtected: true, + provider: { + providerNumber: "11111", + address: { + streetAddress: "123 Test Lane", + city: "Columbus", + state: "OH", + zip: "43212", + }, + }, + }; + + // Act + actions.saveServiceLocation(context, serviceLocation); + + // Assert + expect(commit).toHaveBeenCalledWith(storeMutations.RESET_SCHEDULE); + }); + + it("saveServiceLocation, should reset if provider is different", () => { + // Arrange + const context = { + state: { + order: { + serviceLocation: { + address: "123 Test Lane", + city: "Columbus", + zipCode: "43212", + state: "OH", + zipCodeCtu: "01820", + appointmentType: "Mobile", + isVehicleProtected: true, + provider: { + providerNumber: "11111", + address: { + streetAddress: "123 Test Lane", + city: "Columbus", + state: "OH", + zip: "43212", + }, + }, + }, + }, + }, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + const serviceLocation = { + address: "123 Test Lane", + city: "Columbus", + zipCode: "43212", + state: "OH", + zipCodeCtu: "01820", + appointmentType: "Mobile", + isVehicleProtected: true, + provider: { + providerNumber: "22222", + address: { + streetAddress: "321 Test Lane", + city: "Columbus", + state: "OH", + zip: "43212", + }, + }, + }; + + // Act + actions.saveServiceLocation(context, serviceLocation); + + // Assert + expect(commit).toHaveBeenCalledWith(storeMutations.RESET_SCHEDULE); + }); + + it("saveServiceLocation, should reset if appointment type is different", () => { + // Arrange + const context = { + state: { + order: { + serviceLocation: { + address: "123 Test Lane", + city: "Columbus", + zipCode: "43212", + state: "OH", + zipCodeCtu: "01820", + appointmentType: "Mobile", + isVehicleProtected: true, + provider: { + providerNumber: "11111", + address: { + streetAddress: "123 Test Lane", + city: "Columbus", + state: "OH", + zip: "43212", + }, + }, + }, + }, + }, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + const serviceLocation = { + address: "123 Test Lane", + city: "Columbus", + zipCode: "43212", + state: "OH", + zipCodeCtu: "01820", + appointmentType: "Inshop", + isVehicleProtected: true, + provider: { + providerNumber: "11111", + address: { + streetAddress: "123 Test Lane", + city: "Columbus", + state: "OH", + zip: "43212", + }, + }, + }; + + // Act + actions.saveServiceLocation(context, serviceLocation); + + // Assert + expect(commit).toHaveBeenCalledWith(storeMutations.RESET_SCHEDULE); + }); + + it("saveServiceLocation, should not reset if parameters are the same", () => { + // Arrange + const context = { + state: { + order: { + serviceLocation: { + address: "123 Test Lane", + city: "Columbus", + zipCode: "43212", + state: "OH", + zipCodeCtu: "01820", + appointmentType: "Mobile", + isVehicleProtected: true, + provider: { + providerNumber: "11111", + address: { + streetAddress: "123 Test Lane", + city: "Columbus", + state: "OH", + zip: "43212", + }, + }, + }, + }, + }, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + const serviceLocation = { + address: "123 Test Lane", + city: "Columbus", + zipCode: "43212", + state: "OH", + zipCodeCtu: "01820", + appointmentType: "Mobile", + isVehicleProtected: true, + provider: { + providerNumber: "11111", + address: { + streetAddress: "123 Test Lane", + city: "Columbus", + state: "OH", + zip: "43212", + }, + }, + }; + + // Act + actions.saveServiceLocation(context, serviceLocation); + + // Assert + expect(commit).not.toHaveBeenCalledWith(storeMutations.RESET_SCHEDULE); + }); + it("saveGlassParts, should call mutation", () => { // Arrange const context = { @@ -966,14 +1289,88 @@ describe("Actions", () => { }; const commit = jest.fn(); + const dispatch = jest.fn(); context.commit = commit; + context.dispatch = dispatch; // Act actions.saveGlassParts(context, { glassParts: {} }); // Assert expect(commit).toBeCalledWith(storeMutations.UPDATE_GLASS_PARTS, { glassParts: {} }); + expect(dispatch).toBeCalledWith(storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES); + }); + + it("saveSupportingItems, should call mutations", () => { + // Arrange + const context = { + state: state, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + // Act + actions.saveSupportingItems(context, []); + + // Assert + expect(commit).toBeCalledWith(storeMutations.UPDATE_SUPPORTING_ITEMS, []); + }); + + it("saveSupportingItems, should call reset logic when value is new", () => { + // Arrange + const context = { + state: { + order: { + lineItems: { + supportingItems: ["TestValue1", "TestValue2"], + }, + }, + }, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + // Act + actions.saveSupportingItems(context, ["TestValue3", "TestValue4", "TestValue5"]); + + // Assert + expect(dispatch).toBeCalledWith(storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES); + }); + + it("saveSupportingItems, should not call reset logic when value is the same", () => { + // Arrange + const context = { + state: { + order: { + lineItems: { + supportingItems: ["TestValue1", "TestValue2"], + }, + }, + }, + }; + + const commit = jest.fn(); + const dispatch = jest.fn(); + + context.commit = commit; + context.dispatch = dispatch; + + // Act + actions.saveSupportingItems(context, ["TestValue1", "TestValue2"]); + + // Assert + expect(dispatch).not.toBeCalledWith( + storeActions.RESET_SERVICE_LOCATION_STATE_AND_DEPENDENCIES + ); }); it("clearVin, should call mutation", () => {