Test & mock friendly WebGL detection utility.
npm install webgl-detectorTest & mock friendly WebGL detection utility.
Install
``bash`
npm install --save webgl-detector`
Usagejavascript
import { isWebGLSupported, isWebGL2Supported } from 'webgl-detector';
if (isWebGLSupported()){
// WebGL is supported!
}
// OR for WebGL2
if (isWebGL2Supported()){
// WebGL2 is supported!
}
`
Mocking - Jest & React
Create a file at __mocks__/webgl-detector.js. Make sure __mocks__ directory is adjacent to node_modules. `javascript
// __mocks__/webgl-detector.js
const webglDetector = jest.genMockFromModule('webgl-detector');
webglDetector.setValue = value => {
webglDetector.isSupported = value;
};
webglDetector.isWebGLSupported = () => webglDetector.isSupported;
module.exports = webglDetector;
`AnimationContainer
Suppose we have an Container component named and a renderer container named MyAnimation. If WebGL is supported, AnimationContainer returns MyAnimation component:`javascript
import React from 'react';
import { isWebGLSupported } from 'webgl-detector';
import MyAnimation from './MyAnimation';
const AnimationContainer = props => (
In our test file:
`javascript
describe(' ', () => {
// require mocked module with usual value
beforeEach(() => {
require('webgl-detector').setValue(true);
});
it('should render something if WebGL is supported', () => {
expect(something).toBe(true); // pseudo expect
});
it('should not render something if WebGL is not supported', () => {
// set module mock value for WebGL disabled case
require('webgl-detector').setValue(false);
expect(something).toBe(false); // pseudo expect
});
});
``MIT © Cengiz Can