WickyNilliams / enquire.js

Awesome Media Queries in JavaScript
http://wicky.nillia.ms/enquire.js/
MIT License
3.62k stars 269 forks source link

matchMedia polyfill error #156

Closed Danjavia closed 7 years ago

Danjavia commented 8 years ago

Pls, can you helpme?

I installed a npm dependency that includes enquirejs. But I have issues when try to run tests into my project because

matchMedia not present, legacy browsers require a polyfill

I don't know how to solve this issue.

this is my code

import React from 'react';
import { shallow } from 'enzyme';
import { expect } from 'chai';

// Import components for testing here
import NavbarCrm from '../NavbarCrmComponent';

describe('<NavbarCrm />', function () {

  if (typeof window !== 'undefined') {
    var matchMediaPolyfill = function matchMediaPolyfill(mediaQuery) {
      return {
        media: mediaQuery,
        matches: false,
        addListener: function addListener() {},
        removeListener: function removeListener() {}
      };
    };
    window.matchMedia = window.matchMedia || matchMediaPolyfill;
  }

  it('should have props for heading and children', function (done) {
    const wrapper = shallow(<Menu />);
    expect(wrapper.props().classes).to.be.defined;
    expect(wrapper.props().children).to.be.defined;
    done();
  });
});

I tried in all methods. Above, below but the same error ever.

Can U help me?

I cant to solve the issue putting

if (typeof window !== 'undefined') {
    var matchMediaPolyfill = function matchMediaPolyfill(mediaQuery) {
      return {
        media: mediaQuery,
        matches: false,
        addListener: function addListener() {},
        removeListener: function removeListener() {}
      };
    };
    window.matchMedia = window.matchMedia || matchMediaPolyfill;
  }

into enquirejs/src/include/wrap.js into the dist file for npm installed module. But it isn't a pretty solution.

iDVB commented 7 years ago

I seem to also be having the same issue, and enquire is a dep of a dep of mine. I'm using the paulirish polyfill. https://github.com/paulirish/matchMedia.js/issues/66

WickyNilliams commented 7 years ago

IIRC phantom doesn't include matchMedia. Having done a quick google, seems that this problem has come up before, see #91. Stubbing matchMedia as you have done is the correct solution I guess