player.js
player.js copied to clipboard
Error when fetching the embed code from unit test
Expected Behavior
Video player should be render in an iframe, using react, enzyme and jest framework
Actual Behavior
throws an exception when running jest test
Steps to Reproduce
Here you can see the snippet https://codepen.io/anon/pen/eyZRWN
this is the stack trace and result
RUNS src/__tests__/VideoPlayer.test.js
project/node_modules/react-scripts/scripts/test.js:20
throw err;
^
Error: There was an error fetching the embed code from Vimeo.
at XMLHttpRequest.xhr.onerror (project/node_modules/@vimeo/player/dist/player.js:892:20)
at XMLHttpRequest.callback.(anonymous function) (project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:289:32)
at invokeEventListeners (project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:219:27)
at invokeInlineListeners (project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:166:7)
at EventTargetImpl._dispatch (project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:122:7)
at EventTargetImpl.dispatchEvent (project/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
at XMLHttpRequest.dispatchEvent (project/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:61:35)
at XMLHttpRequest.abort (project/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:405:16)
at Object.abort (project/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:315:13)
at RequestManager.close (project/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:146:21)
at Window.close (project/node_modules/jsdom/lib/jsdom/browser/Window.js:362:29)
at JSDOMEnvironment.dispose (project/node_modules/jest-environment-jsdom/build/index.js:44:19)
at Promise.resolve.then (project/node_modules/jest/node_modules/jest-cli/build/runTest.js:102:17)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
Your example does not appear to trigger the error you're indicating.
However, you can try mocking XMLHttpRequest during tests that involve the player so it does not actually call the API.
Example:
const idVideo = 1234567890;
describe("Video Player", () => {
const XMLHttpRequest = window.XMLHttpRequest;
beforeEach(() => {
const XHRMock = () => ({
open: jest.fn(),
responseText: JSON.stringify({
id: idVideo,
html: `<iframe src="https://player.vimeo.com/video/${idVideo}"></iframe>"`
}),
send: function() {
this.onload();
}
});
window.XMLHttpRequest = jest.fn().mockImplementation(XHRMock);
});
afterEach(() => {
window.XMLHttpRequest = XMLHttpRequest;
});
it("renders player iframe", () => {
const wrapper = mount(<VideoPlayer id={idVideo} />);
// Need to wait for things to tick for the iframe to get dumped into the DOM
return new Promise((resolve, reject) => {
setTimeout(() => {
wrapper.update();
const iframes = wrapper.getDOMNode().getElementsByTagName("iframe");
expect(iframes).toHaveLength(1);
resolve();
});
});
});
});
Thanks @mhazy ! This is the way to go.
The request to Vimeo for getting the embed code seems to have been blocked in that test. Not specific to this library I think.