Commit 738b952b authored by Christoph Häfner's avatar Christoph Häfner

Add final tests for createMovieCollectionByFile

parent f1dfb175
Pipeline #446 passed with stages
in 10 minutes and 41 seconds
......@@ -16,13 +16,13 @@ export const addMovieCollectionWithMoviesLineByLine = movieCollection => {
export const createMovieCollectionByFile = (movieCollection, worker) => {
const workerConstructor = worker || Worker;
return (dispatch) => {
return (dispatch, workerObject) => {
dispatch(addMovieCollectionByFileStart(movieCollection));
// start WebWorker and Dispatch updateMovieCollectionByFileFinished if returns with result
const worker = new workerConstructor("/static/minifiedWorker/parseVideoDbXml.js");
const worker = workerObject || new workerConstructor("/static/minifiedWorker/parseVideoDbXml.js");
worker.postMessage(movieCollection.file);
worker.onmessage = (e) => {
if(e.data.event && e.data.event === "finished") {
if(e.data && e.data.event && e.data.event === "finished") {
dispatch(addMovieCollectionByFileFinished({
name: movieCollection.name,
}));
......
......@@ -8,20 +8,20 @@ describe('movieCollections actions', () => {
name: 'Hugo Blurays',
file: "<xml></xml>"
};
const NOP = () => {};
const NOP = () => { };
const defaultWorkerMock = {
postMessage: NOP
}
const defaultWorkerConstructor = () => {
return defaultWorkerMock;
}
function getWorkerMockObject() {
return Object.assign({}, defaultWorkerMock);
}
it('should dispatch add movie collection by file start', () => {
const workerMock = () => {
return {
postMessage: NOP
}
}
const mockDispatch = jest.fn();
Actions.createMovieCollectionByFile(defaultMovieCollection, workerMock)(mockDispatch);
expect(mockDispatch).toHaveBeenCalledWith({
type: Actions.ADD_MOVIECOLLECTION_BY_FILE_START,
id: 673655561,
movieCollection: defaultMovieCollection
});
Actions.createMovieCollectionByFile(defaultMovieCollection, defaultWorkerConstructor)(mockDispatch);
expect(mockDispatch).toHaveBeenCalledWith(Actions.addMovieCollectionByFileStart(defaultMovieCollection));
})
it('should instantiate worker with file path', () => {
......@@ -45,6 +45,38 @@ describe('movieCollections actions', () => {
Actions.createMovieCollectionByFile(defaultMovieCollection, workerMock)(NOP);
expect(mockPostMessage).toHaveBeenCalledWith(defaultMovieCollection.file);
})
});
it('should set onmessage on worker to receive updates', () => {
const workerObject = getWorkerMockObject();
Actions.createMovieCollectionByFile(defaultMovieCollection, defaultWorkerConstructor)(NOP, workerObject);
chaiExpect(workerObject).to.have.own.property('onmessage');
})
it('should dispatch update event if onmessage is called with data', () => {
const mockDispatch = jest.fn();
const workerObject = getWorkerMockObject();
const movieName = "Matrix";
Actions.createMovieCollectionByFile(defaultMovieCollection, defaultWorkerConstructor)(mockDispatch, workerObject);
workerObject.onmessage({
data: movieName
});
expect(mockDispatch).toHaveBeenCalledWith(Actions.updateMovieCollectionByFile({
name: defaultMovieCollection.name,
movies: [movieName]
}));
})
it('should dispatch finished event if onmessage is called with finish', () => {
const mockDispatch = jest.fn();
const workerObject = getWorkerMockObject();
const movieName = "Matrix";
Actions.createMovieCollectionByFile(defaultMovieCollection, defaultWorkerConstructor)(mockDispatch, workerObject);
workerObject.onmessage({
data: {
event: "finished"
}
});
expect(mockDispatch).toHaveBeenCalledWith(Actions.addMovieCollectionByFileFinished(defaultMovieCollection));
})
});
})
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