Commit 7e55ac02 authored by Winnie Hellmann's avatar Winnie Hellmann Committed by Phil Hughes

Fix timing in geo_nodes/components/app_spec.js

parent 8f693d3c
...@@ -83,7 +83,7 @@ export default { ...@@ -83,7 +83,7 @@ export default {
}); });
}, },
fetchGeoNodes() { fetchGeoNodes() {
this.service return this.service
.getGeoNodes() .getGeoNodes()
.then(res => res.data) .then(res => res.data)
.then(nodes => { .then(nodes => {
...@@ -92,9 +92,7 @@ export default { ...@@ -92,9 +92,7 @@ export default {
}) })
.catch(() => { .catch(() => {
this.isLoading = false; this.isLoading = false;
Flash( Flash(s__('GeoNodes|Something went wrong while fetching nodes'));
s__('GeoNodes|Something went wrong while fetching nodes'),
);
}); });
}, },
fetchNodeDetails(node) { fetchNodeDetails(node) {
...@@ -109,10 +107,7 @@ export default { ...@@ -109,10 +107,7 @@ export default {
primaryRevision: primaryNodeVersion.revision, primaryRevision: primaryNodeVersion.revision,
}); });
this.store.setNodeDetails(nodeId, updatedNodeDetails); this.store.setNodeDetails(nodeId, updatedNodeDetails);
eventHub.$emit( eventHub.$emit('nodeDetailsLoaded', this.store.getNodeDetails(nodeId));
'nodeDetailsLoaded',
this.store.getNodeDetails(nodeId),
);
}) })
.catch(err => { .catch(err => {
if (err.response && err.response.data) { if (err.response && err.response.data) {
...@@ -124,10 +119,7 @@ export default { ...@@ -124,10 +119,7 @@ export default {
sync_status_unavailable: true, sync_status_unavailable: true,
storage_shards_match: null, storage_shards_match: null,
}); });
eventHub.$emit( eventHub.$emit('nodeDetailsLoaded', this.store.getNodeDetails(nodeId));
'nodeDetailsLoaded',
this.store.getNodeDetails(nodeId),
);
} else { } else {
eventHub.$emit('nodeDetailsLoadFailed', nodeId, err); eventHub.$emit('nodeDetailsLoadFailed', nodeId, err);
} }
...@@ -135,14 +127,11 @@ export default { ...@@ -135,14 +127,11 @@ export default {
}, },
repairNode(targetNode) { repairNode(targetNode) {
this.setNodeActionStatus(targetNode, true); this.setNodeActionStatus(targetNode, true);
this.service return this.service
.repairNode(targetNode) .repairNode(targetNode)
.then(() => { .then(() => {
this.setNodeActionStatus(targetNode, false); this.setNodeActionStatus(targetNode, false);
Flash( Flash(s__('GeoNodes|Node Authentication was successfully repaired.'), 'notice');
s__('GeoNodes|Node Authentication was successfully repaired.'),
'notice',
);
}) })
.catch(() => { .catch(() => {
this.setNodeActionStatus(targetNode, false); this.setNodeActionStatus(targetNode, false);
...@@ -151,7 +140,7 @@ export default { ...@@ -151,7 +140,7 @@ export default {
}, },
toggleNode(targetNode) { toggleNode(targetNode) {
this.setNodeActionStatus(targetNode, true); this.setNodeActionStatus(targetNode, true);
this.service return this.service
.toggleNode(targetNode) .toggleNode(targetNode)
.then(res => res.data) .then(res => res.data)
.then(node => { .then(node => {
...@@ -162,14 +151,12 @@ export default { ...@@ -162,14 +151,12 @@ export default {
}) })
.catch(() => { .catch(() => {
this.setNodeActionStatus(targetNode, false); this.setNodeActionStatus(targetNode, false);
Flash( Flash(s__('GeoNodes|Something went wrong while changing node status'));
s__('GeoNodes|Something went wrong while changing node status'),
);
}); });
}, },
removeNode(targetNode) { removeNode(targetNode) {
this.setNodeActionStatus(targetNode, true); this.setNodeActionStatus(targetNode, true);
this.service return this.service
.removeNode(targetNode) .removeNode(targetNode)
.then(() => { .then(() => {
this.store.removeNode(targetNode); this.store.removeNode(targetNode);
......
...@@ -8,7 +8,13 @@ import GeoNodesStore from 'ee/geo_nodes/store/geo_nodes_store'; ...@@ -8,7 +8,13 @@ import GeoNodesStore from 'ee/geo_nodes/store/geo_nodes_store';
import GeoNodesService from 'ee/geo_nodes/service/geo_nodes_service'; import GeoNodesService from 'ee/geo_nodes/service/geo_nodes_service';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import mountComponent from 'spec/helpers/vue_mount_component_helper';
import { NODE_ACTIONS } from 'ee/geo_nodes/constants'; import { NODE_ACTIONS } from 'ee/geo_nodes/constants';
import { PRIMARY_VERSION, NODE_DETAILS_PATH, mockNodes, mockNode, rawMockNodeDetails } from '../mock_data'; import {
PRIMARY_VERSION,
NODE_DETAILS_PATH,
mockNodes,
mockNode,
rawMockNodeDetails,
} from '../mock_data';
const createComponent = () => { const createComponent = () => {
const Component = Vue.extend(appComponent); const Component = Vue.extend(appComponent);
...@@ -86,187 +92,244 @@ describe('AppComponent', () => { ...@@ -86,187 +92,244 @@ describe('AppComponent', () => {
}); });
describe('fetchGeoNodes', () => { describe('fetchGeoNodes', () => {
it('calls service.getGeoNodes and sets response to the store on success', (done) => { it('calls service.getGeoNodes and sets response to the store on success', done => {
spyOn(vm.store, 'setNodes'); spyOn(vm.store, 'setNodes');
vm.fetchGeoNodes(); vm
setTimeout(() => { .fetchGeoNodes()
expect(vm.store.setNodes).toHaveBeenCalledWith(mockNodes); .then(() => {
expect(vm.isLoading).toBe(false); expect(vm.store.setNodes).toHaveBeenCalledWith(mockNodes);
done(); expect(vm.isLoading).toBe(false);
}, 0); })
.then(done)
.catch(done.fail);
}); });
it('sets error flag and message on failure', (done) => { it('sets error flag and message on failure', done => {
response = 'Something went wrong'; response = 'Something went wrong';
statusCode = 500; statusCode = 500;
vm.fetchGeoNodes(); vm
setTimeout(() => { .fetchGeoNodes()
expect(vm.isLoading).toBe(false); .then(() => {
expect(document.querySelector('.flash-text').innerText.trim()).toBe('Something went wrong while fetching nodes'); expect(vm.isLoading).toBe(false);
done(); expect(document.querySelector('.flash-text').innerText.trim()).toBe(
}, 0); 'Something went wrong while fetching nodes',
);
})
.then(done)
.catch(done.fail);
}); });
}); });
describe('fetchNodeDetails', () => { describe('fetchNodeDetails', () => {
it('calls service.getGeoNodeDetails and sets response to the store on success', (done) => { it('calls service.getGeoNodeDetails and sets response to the store on success', done => {
mock.onGet(mockNode.statusPath).reply(200, rawMockNodeDetails); mock.onGet(mockNode.statusPath).reply(200, rawMockNodeDetails);
spyOn(vm.service, 'getGeoNodeDetails').and.callThrough(); spyOn(vm.service, 'getGeoNodeDetails').and.callThrough();
vm.fetchNodeDetails(mockNode); vm
setTimeout(() => { .fetchNodeDetails(mockNode)
expect(vm.service.getGeoNodeDetails).toHaveBeenCalled(); .then(() => {
expect(Object.keys(vm.store.state.nodeDetails).length).not.toBe(0); expect(vm.service.getGeoNodeDetails).toHaveBeenCalled();
expect(vm.store.state.nodeDetails['1']).toBeDefined(); expect(Object.keys(vm.store.state.nodeDetails).length).not.toBe(0);
done(); expect(vm.store.state.nodeDetails['1']).toBeDefined();
}, 0); })
.then(done)
.catch(done.fail);
}); });
it('emits `nodeDetailsLoaded` event with fake nodeDetails object on 404 failure', (done) => { it('emits `nodeDetailsLoaded` event with fake nodeDetails object on 404 failure', done => {
spyOn(eventHub, '$emit'); spyOn(eventHub, '$emit');
mock.onGet(mockNode.statusPath).reply(404, {}); mock.onGet(mockNode.statusPath).reply(404, {});
spyOn(vm.service, 'getGeoNodeDetails').and.callThrough(); spyOn(vm.service, 'getGeoNodeDetails').and.callThrough();
vm.fetchNodeDetails(mockNode); vm
setTimeout(() => { .fetchNodeDetails(mockNode)
expect(eventHub.$emit).toHaveBeenCalledWith('nodeDetailsLoaded', jasmine.any(Object)); .then(() => {
const nodeDetails = vm.store.state.nodeDetails['1']; expect(eventHub.$emit).toHaveBeenCalledWith('nodeDetailsLoaded', jasmine.any(Object));
expect(nodeDetails).toBeDefined(); const nodeDetails = vm.store.state.nodeDetails['1'];
expect(nodeDetails.syncStatusUnavailable).toBe(true); expect(nodeDetails).toBeDefined();
expect(nodeDetails.health).toBe('Request failed with status code 404'); expect(nodeDetails.syncStatusUnavailable).toBe(true);
done(); expect(nodeDetails.health).toBe('Request failed with status code 404');
}, 0); })
.then(done)
.catch(done.fail);
}); });
it('emits `nodeDetailsLoaded` event with fake nodeDetails object on 500 failure', (done) => { it('emits `nodeDetailsLoaded` event with fake nodeDetails object on 500 failure', done => {
spyOn(eventHub, '$emit'); spyOn(eventHub, '$emit');
mock.onGet(mockNode.statusPath).reply(500, {}); mock.onGet(mockNode.statusPath).reply(500, {});
spyOn(vm.service, 'getGeoNodeDetails').and.callThrough(); spyOn(vm.service, 'getGeoNodeDetails').and.callThrough();
vm.fetchNodeDetails(mockNode); vm
setTimeout(() => { .fetchNodeDetails(mockNode)
expect(eventHub.$emit).toHaveBeenCalledWith('nodeDetailsLoaded', jasmine.any(Object)); .then(() => {
const nodeDetails = vm.store.state.nodeDetails['1']; expect(eventHub.$emit).toHaveBeenCalledWith('nodeDetailsLoaded', jasmine.any(Object));
expect(nodeDetails).toBeDefined(); const nodeDetails = vm.store.state.nodeDetails['1'];
expect(nodeDetails.syncStatusUnavailable).toBe(true); expect(nodeDetails).toBeDefined();
expect(nodeDetails.health).toBe('Request failed with status code 500'); expect(nodeDetails.syncStatusUnavailable).toBe(true);
done(); expect(nodeDetails.health).toBe('Request failed with status code 500');
}, 0); })
.then(done)
.catch(done.fail);
}); });
it('emits `nodeDetailsLoadFailed` event on failure when there is no response', (done) => { it('emits `nodeDetailsLoadFailed` event on failure when there is no response', done => {
spyOn(eventHub, '$emit'); spyOn(eventHub, '$emit');
mock.onGet(mockNode.statusPath).reply(500, null); mock.onGet(mockNode.statusPath).reply(500, null);
spyOn(vm.service, 'getGeoNodeDetails').and.callThrough(); spyOn(vm.service, 'getGeoNodeDetails').and.callThrough();
vm.fetchNodeDetails(mockNode); vm
setTimeout(() => { .fetchNodeDetails(mockNode)
expect(eventHub.$emit).toHaveBeenCalledWith('nodeDetailsLoadFailed', mockNode.id, jasmine.any(Object)); .then(() => {
done(); expect(eventHub.$emit).toHaveBeenCalledWith(
}, 0); 'nodeDetailsLoadFailed',
mockNode.id,
jasmine.any(Object),
);
done();
})
.then(done)
.catch(done.fail);
}); });
}); });
describe('repairNode', () => { describe('repairNode', () => {
it('calls service.repairNode and shows success Flash message on request success', (done) => { it('calls service.repairNode and shows success Flash message on request success', done => {
const node = { ...mockNode }; const node = { ...mockNode };
mock.onPost(node.repairPath).reply(200); mock.onPost(node.repairPath).reply(() => {
expect(node.nodeActionActive).toBe(true);
return [200];
});
spyOn(vm.service, 'repairNode').and.callThrough(); spyOn(vm.service, 'repairNode').and.callThrough();
vm.repairNode(node); vm
expect(node.nodeActionActive).toBe(true); .repairNode(node)
setTimeout(() => { .then(() => {
expect(vm.service.repairNode).toHaveBeenCalledWith(node); expect(vm.service.repairNode).toHaveBeenCalledWith(node);
expect(document.querySelector('.flash-text').innerText.trim()).toBe('Node Authentication was successfully repaired.'); expect(document.querySelector('.flash-text').innerText.trim()).toBe(
expect(node.nodeActionActive).toBe(false); 'Node Authentication was successfully repaired.',
done(); );
}, 0); expect(node.nodeActionActive).toBe(false);
})
.then(done)
.catch(done.fail);
}); });
it('calls service.repairNode and shows failure Flash message on request failure', (done) => { it('calls service.repairNode and shows failure Flash message on request failure', done => {
const node = { ...mockNode }; const node = { ...mockNode };
mock.onPost(node.repairPath).reply(500); mock.onPost(node.repairPath).reply(() => {
expect(node.nodeActionActive).toBe(true);
return [500];
});
spyOn(vm.service, 'repairNode').and.callThrough(); spyOn(vm.service, 'repairNode').and.callThrough();
vm.repairNode(node); vm
setTimeout(() => { .repairNode(node)
expect(vm.service.repairNode).toHaveBeenCalledWith(node); .then(() => {
expect(document.querySelector('.flash-text').innerText.trim()).toBe('Something went wrong while repairing node'); expect(vm.service.repairNode).toHaveBeenCalledWith(node);
expect(node.nodeActionActive).toBe(false); expect(document.querySelector('.flash-text').innerText.trim()).toBe(
done(); 'Something went wrong while repairing node',
}, 0); );
expect(node.nodeActionActive).toBe(false);
})
.then(done)
.catch(done.fail);
}); });
}); });
describe('toggleNode', () => { describe('toggleNode', () => {
it('calls service.toggleNode for enabling node and updates toggle button on request success', (done) => { it('calls service.toggleNode for enabling node and updates toggle button on request success', done => {
const node = { ...mockNode }; const node = { ...mockNode };
mock.onPut(node.basePath).reply(200, { mock.onPut(node.basePath).reply(() => {
enabled: true, expect(node.nodeActionActive).toBe(true);
return [
200,
{
enabled: true,
},
];
}); });
spyOn(vm.service, 'toggleNode').and.callThrough(); spyOn(vm.service, 'toggleNode').and.callThrough();
node.enabled = false; node.enabled = false;
vm.toggleNode(node); vm
expect(node.nodeActionActive).toBe(true); .toggleNode(node)
setTimeout(() => { .then(() => {
expect(vm.service.toggleNode).toHaveBeenCalledWith(node); expect(vm.service.toggleNode).toHaveBeenCalledWith(node);
expect(node.enabled).toBe(true); expect(node.enabled).toBe(true);
expect(node.nodeActionActive).toBe(false); expect(node.nodeActionActive).toBe(false);
done(); })
}, 0); .then(done)
.catch(done.fail);
}); });
it('calls service.toggleNode and shows Flash error on request failure', (done) => { it('calls service.toggleNode and shows Flash error on request failure', done => {
const node = { ...mockNode }; const node = { ...mockNode };
mock.onPut(node.basePath).reply(500); mock.onPut(node.basePath).reply(() => {
expect(node.nodeActionActive).toBe(true);
return [500];
});
spyOn(vm.service, 'toggleNode').and.callThrough(); spyOn(vm.service, 'toggleNode').and.callThrough();
node.enabled = false; node.enabled = false;
vm.toggleNode(node); vm
expect(node.nodeActionActive).toBe(true); .toggleNode(node)
setTimeout(() => { .then(() => {
expect(vm.service.toggleNode).toHaveBeenCalledWith(node); expect(vm.service.toggleNode).toHaveBeenCalledWith(node);
expect(document.querySelector('.flash-text').innerText.trim()).toBe('Something went wrong while changing node status'); expect(document.querySelector('.flash-text').innerText.trim()).toBe(
expect(node.nodeActionActive).toBe(false); 'Something went wrong while changing node status',
done(); );
}, 0); expect(node.nodeActionActive).toBe(false);
})
.then(done)
.catch(done.fail);
}); });
}); });
describe('removeNode', () => { describe('removeNode', () => {
it('calls service.removeNode for removing node and shows Flash message on request success', (done) => { it('calls service.removeNode for removing node and shows Flash message on request success', done => {
const node = { ...mockNode }; const node = { ...mockNode };
mock.onDelete(node.basePath).reply(200); mock.onDelete(node.basePath).reply(() => {
expect(node.nodeActionActive).toBe(true);
return [200];
});
spyOn(vm.service, 'removeNode').and.callThrough(); spyOn(vm.service, 'removeNode').and.callThrough();
spyOn(vm.store, 'removeNode').and.stub(); spyOn(vm.store, 'removeNode').and.stub();
vm.removeNode(node); vm
expect(node.nodeActionActive).toBe(true); .removeNode(node)
setTimeout(() => { .then(() => {
expect(vm.service.removeNode).toHaveBeenCalledWith(node); expect(vm.service.removeNode).toHaveBeenCalledWith(node);
expect(vm.store.removeNode).toHaveBeenCalledWith(node); expect(vm.store.removeNode).toHaveBeenCalledWith(node);
expect(document.querySelector('.flash-text').innerText.trim()).toBe('Node was successfully removed.'); expect(document.querySelector('.flash-text').innerText.trim()).toBe(
done(); 'Node was successfully removed.',
}, 0); );
})
.then(done)
.catch(done.fail);
}); });
it('calls service.removeNode and shows Flash message on request failure', (done) => { it('calls service.removeNode and shows Flash message on request failure', done => {
const node = { ...mockNode }; const node = { ...mockNode };
mock.onDelete(node.basePath).reply(500); mock.onDelete(node.basePath).reply(() => {
expect(node.nodeActionActive).toBe(true);
return [500];
});
spyOn(vm.service, 'removeNode').and.callThrough(); spyOn(vm.service, 'removeNode').and.callThrough();
spyOn(vm.store, 'removeNode').and.stub(); spyOn(vm.store, 'removeNode').and.stub();
vm.removeNode(node); vm
expect(node.nodeActionActive).toBe(true); .removeNode(node)
setTimeout(() => { .then(() => {
expect(vm.service.removeNode).toHaveBeenCalledWith(node); expect(vm.service.removeNode).toHaveBeenCalledWith(node);
expect(vm.store.removeNode).not.toHaveBeenCalled(); expect(vm.store.removeNode).not.toHaveBeenCalled();
expect(document.querySelector('.flash-text').innerText.trim()).toBe('Something went wrong while removing node'); expect(document.querySelector('.flash-text').innerText.trim()).toBe(
done(); 'Something went wrong while removing node',
}, 0); );
})
.then(done)
.catch(done.fail);
}); });
}); });
...@@ -399,7 +462,9 @@ describe('AppComponent', () => { ...@@ -399,7 +462,9 @@ describe('AppComponent', () => {
it('renders loading animation when `isLoading` is true', () => { it('renders loading animation when `isLoading` is true', () => {
vm.isLoading = true; vm.isLoading = true;
expect(vm.$el.querySelectorAll('.loading-animation.prepend-top-20.append-bottom-20').length).not.toBe(0); expect(
vm.$el.querySelectorAll('.loading-animation.prepend-top-20.append-bottom-20').length,
).not.toBe(0);
}); });
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment