Update unit tests to account for mocking out/ignoring google maps script

This commit is contained in:
Alex Humphries 2026-03-05 10:05:29 -05:00
parent 8c1991a5a5
commit a1cdf7b630
2 changed files with 123 additions and 108 deletions

View file

@ -10,11 +10,72 @@ import { getRandomString, getRandomInt, getRandomBoolean } from '@/helpers/data-
import navigationScenarios from '@/router/router-constants/navigation-scenarios.js'; import navigationScenarios from '@/router/router-constants/navigation-scenarios.js';
import { useMainStore } from '@/store/index.js'; import { useMainStore } from '@/store/index.js';
/** @ignore */
function setupMocks({
storeData,
props,
isShallowMount = true,
querySelectorFunction,
geocoderResult = ['1234 Test Street']
}) {
const mountOptions = getMountOptions({
router: {
navigate: jest.fn(),
navigateWithSpinner: jest.fn()
},
loadScript: jest.fn().mockResolvedValue()
});
if (storeData) mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: storeData
}
})];
window.google = {
maps: {
event: {
addListener: jest
.fn()
.mockImplementation((element, eventName, callbackFunction) => {
/** @ignore */
function interceptedCallbackFunction(e) {
callbackFunction(e.detail);
}
// selectedPlace = "Woogly";
element.addEventListener(eventName, interceptedCallbackFunction);
}),
removeListener: jest.fn(),
clearInstanceListeners: jest.fn()
},
places: {
Autocomplete: jest.fn().mockImplementation((el) => el),
AutocompleteService: jest.fn().mockImplementation(() => {
return {
getPlacePredictions: (request, callback) => {
// Throwing an error here guarantees that the function that calls it finishes
throw new Error();
}
};
}),
PlacesService: jest.fn().mockImplementation(() => {}),
AutocompleteSessionToken: jest.fn().mockImplementation(() => {})
}
}
};
if (props) mountOptions.propsData = props;
const wrapper = shallowMount(contactDetails, mountOptions);
return { wrapper };
}
describe('contactDetails.vue', () => { describe('contactDetails.vue', () => {
describe('Rendering', () => { describe('Rendering', () => {
test('Should render site header', () => { test('Should render site header', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const siteHeader = wrapper.findComponent({ ref: 'siteHeader' }); const siteHeader = wrapper.findComponent({ ref: 'siteHeader' });
@ -24,7 +85,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render sub title', () => { test('Should render sub title', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const siteSubHeader = wrapper.findComponent({ ref: 'siteSubHeader' }); const siteSubHeader = wrapper.findComponent({ ref: 'siteSubHeader' });
@ -34,7 +95,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render appointment information alert', () => { test('Should render appointment information alert', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const appointmentInformationAlert = wrapper.findComponent({ ref: 'appointmentInformationAlert' }); const appointmentInformationAlert = wrapper.findComponent({ ref: 'appointmentInformationAlert' });
@ -44,8 +105,6 @@ describe('contactDetails.vue', () => {
}); });
test('Should render same as policy address question subcomponent if service zip is same as customer zip', async () => { test('Should render same as policy address question subcomponent if service zip is same as customer zip', async () => {
// Arrange // Arrange
const mountOptions = getMountOptions();
const customerStreetAddress = getRandomString(10, 50); const customerStreetAddress = getRandomString(10, 50);
const customerStreetAddress2 = getRandomString(0, 50); const customerStreetAddress2 = getRandomString(0, 50);
const customerCity = getRandomString(4, 20); const customerCity = getRandomString(4, 20);
@ -55,7 +114,7 @@ describe('contactDetails.vue', () => {
const serviceCity = getRandomString(4, 20); const serviceCity = getRandomString(4, 20);
const serviceState = getRandomString(2, 2); const serviceState = getRandomString(2, 2);
const zipCode = getRandomInt(10000, 99999).toString(); const zipCode = getRandomInt(10000, 99999).toString();
const mainInitialState = { const storeData = {
order: { order: {
customer: { customer: {
address: { address: {
@ -75,12 +134,7 @@ describe('contactDetails.vue', () => {
} }
} }
}; };
mountOptions.global.plugins = [createTestingPinia({ const { wrapper } = setupMocks({ storeData });
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(contactDetails, mountOptions);
await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick();
@ -92,8 +146,6 @@ describe('contactDetails.vue', () => {
}); });
test('Should not render same as policy address question subcomponent if service zip is different from customer zip', () => { test('Should not render same as policy address question subcomponent if service zip is different from customer zip', () => {
// Arrange // Arrange
const mountOptions = getMountOptions();
const firstName = getRandomString(4, 15); const firstName = getRandomString(4, 15);
const lastName = getRandomString(4, 15); const lastName = getRandomString(4, 15);
const emailAddress = getRandomString(10, 20); const emailAddress = getRandomString(10, 20);
@ -101,7 +153,7 @@ describe('contactDetails.vue', () => {
const testZipCode = getRandomInt(10000, 99999); const testZipCode = getRandomInt(10000, 99999);
const customerZipCode = testZipCode.toString(); const customerZipCode = testZipCode.toString();
const serviceZipCode = (testZipCode + 1).toString(); const serviceZipCode = (testZipCode + 1).toString();
const mainInitialState = { const storeData = {
order: { order: {
customer: { customer: {
firstName, firstName,
@ -119,12 +171,7 @@ describe('contactDetails.vue', () => {
} }
} }
}; };
mountOptions.global.plugins = [createTestingPinia({ const { wrapper } = setupMocks({ storeData });
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(contactDetails, mountOptions);
// Act // Act
const sameAsPolicyAddressQuestion = wrapper.findComponent({ ref: 'sameAsPolicyAddressQuestion' }); const sameAsPolicyAddressQuestion = wrapper.findComponent({ ref: 'sameAsPolicyAddressQuestion' });
@ -134,7 +181,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render address question subcomponent', () => { test('Should render address question subcomponent', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const addressQuestion = wrapper.findComponent({ ref: 'addressQuestion' }); const addressQuestion = wrapper.findComponent({ ref: 'addressQuestion' });
@ -144,7 +191,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render apartment question subcomponent', () => { test('Should render apartment question subcomponent', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const address2Question = wrapper.findComponent({ ref: 'address2Question' }); const address2Question = wrapper.findComponent({ ref: 'address2Question' });
@ -154,7 +201,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render city question subcomponent', () => { test('Should render city question subcomponent', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const cityQuestion = wrapper.findComponent({ ref: 'cityQuestion' }); const cityQuestion = wrapper.findComponent({ ref: 'cityQuestion' });
@ -164,7 +211,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render state question subcomponent', () => { test('Should render state question subcomponent', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const stateQuestion = wrapper.findComponent({ ref: 'stateQuestion' }); const stateQuestion = wrapper.findComponent({ ref: 'stateQuestion' });
@ -174,7 +221,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render zip code question subcomponent', () => { test('Should render zip code question subcomponent', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const zipCodeQuestion = wrapper.findComponent({ ref: 'zipCodeQuestion' }); const zipCodeQuestion = wrapper.findComponent({ ref: 'zipCodeQuestion' });
@ -184,7 +231,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render change zip code alert', () => { test('Should render change zip code alert', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const changeZipCodeAlert = wrapper.findComponent({ ref: 'changeZipCodeAlert' }); const changeZipCodeAlert = wrapper.findComponent({ ref: 'changeZipCodeAlert' });
@ -194,7 +241,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render vehicle protected question subcomponent', () => { test('Should render vehicle protected question subcomponent', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const vehicleProtectedQuestion = wrapper.findComponent({ ref: 'vehicleProtectedQuestion' }); const vehicleProtectedQuestion = wrapper.findComponent({ ref: 'vehicleProtectedQuestion' });
@ -204,7 +251,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render technician notes textarea question subcomponent', () => { test('Should render technician notes textarea question subcomponent', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const notesQuestion = wrapper.findComponent({ ref: 'notesQuestion' }); const notesQuestion = wrapper.findComponent({ ref: 'notesQuestion' });
@ -214,7 +261,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render clearance text subcomponent', () => { test('Should render clearance text subcomponent', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const clearanceText = wrapper.findComponent({ ref: 'clearanceText' }); const clearanceText = wrapper.findComponent({ ref: 'clearanceText' });
@ -224,7 +271,7 @@ describe('contactDetails.vue', () => {
}); });
test('Should render site footer', () => { test('Should render site footer', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions()); const { wrapper } = setupMocks({});
// Act // Act
const footer = wrapper.findComponent({ ref: 'siteFooter' }); const footer = wrapper.findComponent({ ref: 'siteFooter' });
@ -234,8 +281,6 @@ describe('contactDetails.vue', () => {
}); });
test('Mocked store yields expected data', () => { test('Mocked store yields expected data', () => {
// Arrange // Arrange
const mountOptions = getMountOptions();
const address = getRandomString(10, 50); const address = getRandomString(10, 50);
const address2 = getRandomString(0, 50); const address2 = getRandomString(0, 50);
const city = getRandomString(4, 20); const city = getRandomString(4, 20);
@ -243,7 +288,7 @@ describe('contactDetails.vue', () => {
const zipCode = getRandomInt(10000, 99999).toString(); const zipCode = getRandomInt(10000, 99999).toString();
const notesForTechnician = getRandomString(1, 100); const notesForTechnician = getRandomString(1, 100);
const isVehicleProtected = getRandomBoolean(); const isVehicleProtected = getRandomBoolean();
const mainInitialState = { const storeData = {
order: { order: {
serviceLocation: { serviceLocation: {
address, address,
@ -258,13 +303,8 @@ describe('contactDetails.vue', () => {
} }
} }
}; };
mountOptions.global.plugins = [createTestingPinia({
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(contactDetails, mountOptions); const { wrapper } = setupMocks({ storeData });
// Assert // Assert
expect(wrapper.vm.address).toBe(address); expect(wrapper.vm.address).toBe(address);
@ -280,11 +320,7 @@ describe('contactDetails.vue', () => {
describe('Navigation', () => { describe('Navigation', () => {
test('Back button clicked triggers navigation', () => { test('Back button clicked triggers navigation', () => {
// Arrange // Arrange
const wrapper = shallowMount(contactDetails, getMountOptions({ const { wrapper } = setupMocks({});
router: {
navigateWithSpinner: jest.fn()
}
}));
wrapper.vm.navigateBack = baseMixin.methods.navigateBack; wrapper.vm.navigateBack = baseMixin.methods.navigateBack;
// Act // Act
@ -295,41 +331,25 @@ describe('contactDetails.vue', () => {
expect(wrapper.vm.$router.navigateWithSpinner) expect(wrapper.vm.$router.navigateWithSpinner)
.toHaveBeenCalledWith(navigationScenarios.CLICKED_BACK, undefined); .toHaveBeenCalledWith(navigationScenarios.CLICKED_BACK, undefined);
}); });
test('Forward button clicked triggers appropriate navigation', () => { test('Forward button clicked triggers appropriate navigation', async () => {
// Arrange // Arrange
const mountOptions = getMountOptions({ const storeData = {
router: {
navigate: jest.fn()
},
navigationScenarios
});
const mainInitialState = {
order: { order: {
serviceLocation: { IsSafeliteProvider: true } serviceLocation: { IsSafeliteProvider: true }
} }
}; };
mountOptions.global.plugins = [createTestingPinia({ const { wrapper } = setupMocks({ storeData });
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(contactDetails, mountOptions);
// Act // Act
wrapper.vm.forwardButtonAction(); await wrapper.vm.forwardButtonAction();
// Assert // Assert
expect(wrapper.vm.$router.navigate).toHaveBeenCalled(); expect(wrapper.vm.$router.navigate).toHaveBeenCalled();
expect(wrapper.vm.$router.navigate) expect(wrapper.vm.$router.navigate)
.toHaveBeenCalledWith(navigationScenarios.CLICKED_FORWARD, undefined); .toHaveBeenCalledWith(navigationScenarios.CLICKED_FORWARD, undefined);
}); });
test('Forward button click updates service location info', () => { test('Forward button click updates service location info', async () => {
// Arrange // Arrange
const mountOptions = getMountOptions({
router: {
navigate: jest.fn()
}
});
const provider = { const provider = {
address: { address: {
city: getRandomString(4, 20), city: getRandomString(4, 20),
@ -342,19 +362,14 @@ describe('contactDetails.vue', () => {
phoneNumber: getRandomInt(1000000000, 9999999999).toString(), phoneNumber: getRandomInt(1000000000, 9999999999).toString(),
providerNumber: getRandomInt(100000, 999999).toString() providerNumber: getRandomInt(100000, 999999).toString()
} }
const mainInitialState = { const storeData = {
order: { order: {
serviceLocation: { serviceLocation: {
provider provider
} }
} }
}; };
mountOptions.global.plugins = [createTestingPinia({ const { wrapper } = setupMocks({ storeData });
initialState: {
main: mainInitialState
}
})];
const wrapper = shallowMount(contactDetails, mountOptions);
const address = getRandomString(10, 50); const address = getRandomString(10, 50);
const address2 = getRandomString(0, 50); const address2 = getRandomString(0, 50);
const city = getRandomString(4, 20); const city = getRandomString(4, 20);
@ -371,7 +386,7 @@ describe('contactDetails.vue', () => {
}); });
// Act // Act
wrapper.vm.forwardButtonAction(); await wrapper.vm.forwardButtonAction();
// Assert // Assert
expect(useMainStore().updateServiceLocation).toHaveBeenCalledWith({ expect(useMainStore().updateServiceLocation).toHaveBeenCalledWith({
@ -383,21 +398,16 @@ describe('contactDetails.vue', () => {
}); });
}); });
test('Forward button click updates notes for technician', () => { test('Forward button click updates notes for technician', async () => {
// Arrange // Arrange
const mountOptions = getMountOptions({ const { wrapper } = setupMocks({});
router: {
navigate: jest.fn()
}
});
const wrapper = shallowMount(contactDetails, mountOptions);
const notesForTechnician = getRandomString(1, 100); const notesForTechnician = getRandomString(1, 100);
wrapper.setData({ wrapper.setData({
notesForTechnician notesForTechnician
}); });
// Act // Act
wrapper.vm.forwardButtonAction(); await wrapper.vm.forwardButtonAction();
// Assert // Assert
expect(useMainStore().updateContactInfo).toHaveBeenCalledWith({ expect(useMainStore().updateContactInfo).toHaveBeenCalledWith({

View file

@ -291,36 +291,41 @@ export default {
const acSessionToken = new window.google.maps.places.AutocompleteSessionToken(); const acSessionToken = new window.google.maps.places.AutocompleteSessionToken();
const addressValue = `${this.address}, ${this.city}, ${this.state} ${this.zipCode}`; const addressValue = `${this.address}, ${this.city}, ${this.state} ${this.zipCode}`;
acService.getPlacePredictions( try {
{ acService.getPlacePredictions(
input: addressValue, {
type: ['geocode'], input: addressValue,
componentRestrictions: { country: ['us'] }, type: ['geocode'],
sessionToken: acSessionToken componentRestrictions: { country: ['us'] },
}, sessionToken: acSessionToken
(predictions) => { },
if (predictions && predictions.length > 0) { (predictions) => {
const firstPrediction = predictions[0]; if (predictions && predictions.length > 0) {
if (firstPrediction.place_id) { const firstPrediction = predictions[0];
placeService.getDetails( if (firstPrediction.place_id) {
{ placeService.getDetails(
placeId: firstPrediction.place_id, {
fields: ['address_components'], placeId: firstPrediction.place_id,
sessionToken: acSessionToken fields: ['address_components'],
}, sessionToken: acSessionToken
(details) => { },
this.fillInAddress(details); (details) => {
resolve(); this.fillInAddress(details);
} resolve();
); }
);
} else {
resolve();
}
} else { } else {
resolve(); resolve();
} }
} else {
resolve();
} }
} );
); } catch (error) {
window.console.warn('Error initializing Google Places API services', error);
resolve();
}
}); });
return autoCompletePromise; return autoCompletePromise;