SSR-776 Fix unit test
This commit is contained in:
parent
4aa6c4ed64
commit
266ebc4e1c
3 changed files with 55 additions and 26 deletions
|
|
@ -20,5 +20,6 @@ Object {
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"infoWindow": null,
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
|
||||||
|
|
@ -21,12 +21,19 @@ const mockGeocoderInstance2 = {
|
||||||
};
|
};
|
||||||
const mockGeocoder = jest.fn(() => (mockGeocoderInstance1));
|
const mockGeocoder = jest.fn(() => (mockGeocoderInstance1));
|
||||||
|
|
||||||
const mockAdvancedMarkerElement = jest.fn();
|
const mockAdvancedMarkerInstance = {
|
||||||
|
addListener: jest.fn()
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockAdvancedMarkerElement = jest.fn(() => (mockAdvancedMarkerInstance));
|
||||||
|
const mockPinElement = jest.fn();
|
||||||
|
const mockInfoWindow = jest.fn();
|
||||||
|
|
||||||
const mockImportLibrary = jest.fn().mockImplementation(() => (Promise.resolve({
|
const mockImportLibrary = jest.fn().mockImplementation(() => (Promise.resolve({
|
||||||
Map: mockMap,
|
Map: mockMap,
|
||||||
Geocoder: mockGeocoder,
|
Geocoder: mockGeocoder,
|
||||||
AdvancedMarkerElement: mockAdvancedMarkerElement
|
AdvancedMarkerElement: mockAdvancedMarkerElement,
|
||||||
|
PinElement: mockPinElement
|
||||||
})));
|
})));
|
||||||
|
|
||||||
const mockExtend = jest.fn();
|
const mockExtend = jest.fn();
|
||||||
|
|
@ -36,7 +43,8 @@ const setupGoogleMock = () => {
|
||||||
global.window.google = {
|
global.window.google = {
|
||||||
maps: {
|
maps: {
|
||||||
importLibrary: mockImportLibrary,
|
importLibrary: mockImportLibrary,
|
||||||
LatLngBounds: jest.fn(() => (mockLatLngBound))
|
LatLngBounds: jest.fn(() => (mockLatLngBound)),
|
||||||
|
InfoWindow: mockInfoWindow
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
@ -46,6 +54,7 @@ beforeEach(() => {
|
||||||
mockMap.mockClear();
|
mockMap.mockClear();
|
||||||
mockGeocoder.mockClear();
|
mockGeocoder.mockClear();
|
||||||
mockAdvancedMarkerElement.mockClear();
|
mockAdvancedMarkerElement.mockClear();
|
||||||
|
mockPinElement.mockClear();
|
||||||
|
|
||||||
// clear methods
|
// clear methods
|
||||||
mockExtend.mockClear();
|
mockExtend.mockClear();
|
||||||
|
|
@ -150,7 +159,13 @@ describe('Google Map', () => {
|
||||||
|
|
||||||
const zipCode = '00882';
|
const zipCode = '00882';
|
||||||
const [address1, address2, position1, position2] = ['a1', 'a2', 'p1', 'p2'];
|
const [address1, address2, position1, position2] = ['a1', 'a2', 'p1', 'p2'];
|
||||||
const addresses = [address1, address2];
|
const providers = [
|
||||||
|
{
|
||||||
|
fullAddress: address1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fullAddress: address2
|
||||||
|
}];
|
||||||
mockGeocode.mockImplementation((obj) => {
|
mockGeocode.mockImplementation((obj) => {
|
||||||
let result = {};
|
let result = {};
|
||||||
if (obj.address.includes(zipCode)) {
|
if (obj.address.includes(zipCode)) {
|
||||||
|
|
@ -171,7 +186,7 @@ describe('Google Map', () => {
|
||||||
return Promise.resolve(result);
|
return Promise.resolve(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
const propsData = { addresses, zipCode };
|
const propsData = { providers, zipCode };
|
||||||
const { wrapper } = getMountedComponent({}, {}, propsData);
|
const { wrapper } = getMountedComponent({}, {}, propsData);
|
||||||
await awaitingSetupTicks(wrapper);
|
await awaitingSetupTicks(wrapper);
|
||||||
|
|
||||||
|
|
@ -198,8 +213,6 @@ describe('Google Map', () => {
|
||||||
test('watch addresses creates map with expected markers', async () => {
|
test('watch addresses creates map with expected markers', async () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
const zipCode = '00882';
|
const zipCode = '00882';
|
||||||
const { wrapper } = getMountedComponent({}, { zipCode });
|
|
||||||
await awaitingSetupTicks(wrapper);
|
|
||||||
|
|
||||||
const mockGetCenter = jest.fn();
|
const mockGetCenter = jest.fn();
|
||||||
const mockNorthEast = jest.fn();
|
const mockNorthEast = jest.fn();
|
||||||
|
|
@ -238,10 +251,18 @@ describe('Google Map', () => {
|
||||||
}
|
}
|
||||||
return Promise.resolve(result);
|
return Promise.resolve(result);
|
||||||
});
|
});
|
||||||
const newAddresses = [address1, address2];
|
const providers = [
|
||||||
|
{
|
||||||
|
fullAddress: address1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fullAddress: address2
|
||||||
|
}];
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await wrapper.vm.$options.watch.addresses.call(wrapper.vm, newAddresses);
|
const { wrapper } = getMountedComponent({}, { zipCode });
|
||||||
|
await awaitingSetupTicks(wrapper);
|
||||||
|
await wrapper.vm.$options.watch.providers.call(wrapper.vm, providers);
|
||||||
await awaitingSetupTicks(wrapper);
|
await awaitingSetupTicks(wrapper);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
|
@ -260,9 +281,9 @@ describe('Google Map', () => {
|
||||||
expect(mockExtend).toBeCalledTimes(6);
|
expect(mockExtend).toBeCalledTimes(6);
|
||||||
expect(mockFitBounds).toBeCalledTimes(2);
|
expect(mockFitBounds).toBeCalledTimes(2);
|
||||||
|
|
||||||
expect(mockGetCenter).toBeCalledTimes(1);
|
expect(mockGetCenter).toBeCalledTimes(2);
|
||||||
expect(mockNorthEast).toBeCalledTimes(1);
|
expect(mockNorthEast).toBeCalledTimes(2);
|
||||||
expect(mockSouthWest).toBeCalledTimes(1);
|
expect(mockSouthWest).toBeCalledTimes(2);
|
||||||
});
|
});
|
||||||
describe('method', () => {
|
describe('method', () => {
|
||||||
test('getMap calls map constructor with expected parameters', async () => {
|
test('getMap calls map constructor with expected parameters', async () => {
|
||||||
|
|
@ -295,6 +316,15 @@ describe('Google Map', () => {
|
||||||
await awaitingSetupTicks(wrapper);
|
await awaitingSetupTicks(wrapper);
|
||||||
const numberOfExtendCallsDuringMount = 2;
|
const numberOfExtendCallsDuringMount = 2;
|
||||||
|
|
||||||
|
mockGeocode.mockImplementation((obj) => {
|
||||||
|
const result = {
|
||||||
|
results: [
|
||||||
|
{ geometry: { location: 'p1' } }
|
||||||
|
]
|
||||||
|
};
|
||||||
|
return Promise.resolve(result);
|
||||||
|
});
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = wrapper.vm.getBounds(locations);
|
const result = wrapper.vm.getBounds(locations);
|
||||||
|
|
||||||
|
|
@ -362,7 +392,7 @@ describe('Google Map', () => {
|
||||||
expect(mockGeocode).toHaveBeenCalledTimes(numberOfCallsFromMount);
|
expect(mockGeocode).toHaveBeenCalledTimes(numberOfCallsFromMount);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await wrapper.vm.getLocationsFromAddresses(addresses);
|
await wrapper.vm.getLocationsFromAddresses(addresses?.map((a) => ({fullAddress: a})));
|
||||||
await wrapper.vm.$nextTick();
|
await wrapper.vm.$nextTick();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
|
@ -374,7 +404,7 @@ describe('Google Map', () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
const wrapper = shallowMount(googleMap, {});
|
const wrapper = shallowMount(googleMap, {});
|
||||||
await awaitingSetupTicks(wrapper);
|
await awaitingSetupTicks(wrapper);
|
||||||
const addresses = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8'];
|
const addresses = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8'].map((a) => ({fullAddress: a}));
|
||||||
mockGeocode.mockImplementationOnce(() => null)
|
mockGeocode.mockImplementationOnce(() => null)
|
||||||
.mockImplementationOnce(() => ({ results: null }))
|
.mockImplementationOnce(() => ({ results: null }))
|
||||||
.mockImplementationOnce(() => ({ results: [] }))
|
.mockImplementationOnce(() => ({ results: [] }))
|
||||||
|
|
@ -402,7 +432,7 @@ describe('Google Map', () => {
|
||||||
const expectedResult = ['some value', 'some other value'];
|
const expectedResult = ['some value', 'some other value'];
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await wrapper.vm.getLocationsFromAddresses(addresses);
|
const result = (await wrapper.vm.getLocationsFromAddresses(addresses)).map(a => a.position);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
expect(result.length).toBe(2);
|
expect(result.length).toBe(2);
|
||||||
|
|
@ -477,7 +507,7 @@ describe('Google Map', () => {
|
||||||
Promise.resolve(obj.address.includes(zipCode)
|
Promise.resolve(obj.address.includes(zipCode)
|
||||||
? zipCodeGeocodeResult
|
? zipCodeGeocodeResult
|
||||||
: shopGeocodeResult));
|
: shopGeocodeResult));
|
||||||
const addresses = ['a1', 'a2'];
|
const addresses = [{fullAddress:'a1'}, {fullAddress:'a2'}];
|
||||||
const { wrapper } = getMountedComponent({}, { zipCode });
|
const { wrapper } = getMountedComponent({}, { zipCode });
|
||||||
await awaitingSetupTicks(wrapper);
|
await awaitingSetupTicks(wrapper);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,11 @@ export default {
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
async providers(newProviders) {
|
async providers(newProviders) {
|
||||||
this.createMapWithMarkersForAddresses(newProviders);
|
await this.createMapWithMarkersForAddresses(newProviders);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
beforeMount() {
|
async beforeMount() {
|
||||||
this.createMapWithMarkersForAddresses(this.providers);
|
await this.createMapWithMarkersForAddresses(this.providers);
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
async getMap(center) {
|
async getMap(center) {
|
||||||
|
|
@ -77,9 +77,7 @@ export default {
|
||||||
<br /> <a href="${directionsUrl}" target="_blank">Get directions</a>
|
<br /> <a href="${directionsUrl}" target="_blank">Get directions</a>
|
||||||
</div>`;
|
</div>`;
|
||||||
|
|
||||||
const infoWindow = new window.google.maps.InfoWindow({
|
const infoWindow = new window.google.maps.InfoWindow({ content });
|
||||||
content
|
|
||||||
});
|
|
||||||
|
|
||||||
marker.addListener('click', () => {
|
marker.addListener('click', () => {
|
||||||
this.infoWindow?.close();
|
this.infoWindow?.close();
|
||||||
|
|
@ -96,15 +94,15 @@ export default {
|
||||||
const geocodeShopAddressPromises = providers?.map((provider) => this.geocoder.geocode({ address: provider.fullAddress })) ?? [];
|
const geocodeShopAddressPromises = providers?.map((provider) => this.geocoder.geocode({ address: provider.fullAddress })) ?? [];
|
||||||
const geocodeShopAddressResults = await Promise.all(geocodeShopAddressPromises);
|
const geocodeShopAddressResults = await Promise.all(geocodeShopAddressPromises);
|
||||||
|
|
||||||
return providers.map((provider, index) => {
|
return providers?.map((provider, index) => {
|
||||||
const { results } = geocodeShopAddressResults[index];
|
const results = geocodeShopAddressResults[index]?.results;
|
||||||
let position = null;
|
let position = null;
|
||||||
if (results && results.length > 0) {
|
if (results && results.length > 0) {
|
||||||
position = results[0].geometry?.location;
|
position = results[0].geometry?.location;
|
||||||
}
|
}
|
||||||
|
|
||||||
return { ...provider, position };
|
return { ...provider, position };
|
||||||
}).filter((provider) => provider.position != null);
|
}).filter((provider) => provider.position != null) ?? [];
|
||||||
},
|
},
|
||||||
async getBoundsFromAddress(address) {
|
async getBoundsFromAddress(address) {
|
||||||
await this.setGeocoder();
|
await this.setGeocoder();
|
||||||
|
|
@ -116,7 +114,7 @@ export default {
|
||||||
async createMapWithMarkersForAddresses(providers) {
|
async createMapWithMarkersForAddresses(providers) {
|
||||||
const providerPositions = await this.getLocationsFromAddresses(providers);
|
const providerPositions = await this.getLocationsFromAddresses(providers);
|
||||||
const zipBounds = await this.getBoundsFromAddress(this.zipCodeAddress);
|
const zipBounds = await this.getBoundsFromAddress(this.zipCodeAddress);
|
||||||
const map = await this.getMap(zipBounds.getCenter());
|
const map = await this.getMap(zipBounds?.getCenter());
|
||||||
|
|
||||||
await this.addMarkersToMap(map, providerPositions);
|
await this.addMarkersToMap(map, providerPositions);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue