Giter Club home page Giter Club logo

mocha-mix's People

Contributors

koulmomo avatar rexk avatar schottra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mocha-mix's Issues

Deprecate withContext

Mocking with withContext introduces extra complexity for mocha-mix with very few benefits.

If the Components needs to be tested with Context Object, recommend users to create ContextWrapperComponent for test components and use them with render method

Examples

Fluxible

import FluxibleComponent from 'fluxible-addons-react/FluxibleComponent';
import {createMockComponentContext} from 'fluxible/utils';
import assert from 'assert';
import MochaMix, { render } from 'mocha-mix';

// do all describe and before and after
...
var context = createMockComponentContext({ stores: [MockStore] });
var fluxibleContext = createMockComponentContext({ stores: [MockStore] });
var TestComponent = MochaMix.mix({ 
  require: './TestComponent'
});

var rendered = render(
  <FluxbileComponent context={context}>
    <TestComponent { ...props } />
  </FluxibleComponent>
);

assert.ok(rendered);
...

Above approach gives following advantages.

  1. Total separation of concerns for testing components
  2. No need to track down "right" ComponentClass instance
  3. Explicit description of what is being tested.

Add API Documentation

Examples are great way to start the module, but extensive API documentation can help out more advanced use cases.

Update README.md

Key phrase to consider:

  1. Mocking with javascript semantics.
  2. server-side testing

TypeError: Cannot read property 'replace' of undefined

assertNotRender throws TypeError: Cannot read property 'replace' of undefined, if it is used with as follows:

MochaMix.assertNotRender(
  <DataGridHeader />,
  'datagrid-td datagrid-flex'
);

This is due to

/**
 * Assert component render to not matches the unexpectedHTML
 * @method assertNotRender
 *
 * @param {ReactComponent} Component class
 * @param {Object} props used to initialize the component
 * @param {String} unexpectedHTML can contain wildcards "+" or "*"
 *
 * @example
 * // The code bellow will raise an error if the component renders
 * // a div with any attributes and the string "something" inside any
 * // inner element.
 * assertNotRender(MyComponent, {}, "<div+>*something*</div>");
 **/
function assertNotRender(Component, props, unexpectedHTML) {
  if (isUndefined(unexpectedHTML)) {
    expectedHTML = props;                      // wrong value assignment
    props = undefined;
  }
  var expected = renderToString(Component, props);
  var regEx = buildRegEx(unexpectedHTML);

  assert(
    !regEx.test(expected),
    'Expected: ' + expected + ' to NOT match: ' + regEx
  );
}

module.exports = {
  renderMatch: assertRender,
  renderNotMatch: assertNotRender
};

Fix it ASAP.

Pass down props to ReactStubElements

https://github.com/rexk/mocha-mix/blob/master/lib/mocha-mix.js#L56

following change will allow all simulated events to be captured through mock classes

function createStubReactClass(tagName, name) {
  return React.createClass({
    displayName: (name || '') + ' stub',
    render: function () {
      return React.createElement(tagName);
    }
  });
}
function createStubReactClass(tagName, name) {
  return React.createClass({
    displayName: (name || '') + ' stub',
    render: function () {
      return React.createElement(
         tagName, 
         this.props, 
         this.props.children
      );
    }
  });
}

Improve mocking syntax

Current mocking syntax

import MochaMix from 'mocha-mix';

MochaMix.mix({
   require: './MyComponent',
   mocks: {
     SubComp1: './SubComp1',
     SubComp2: {
       require: './SubComp2',
       tagName: 'input'
     },
     lodash: {
       require: 'lodash',
       react: false,
       mock: function() { console.log('I am lodash mock') }
     }
   }
});

Proposal

import MochaMix, { generateStub } from 'mocha-mix';
import MyMockComponent from './my-mock';
MochaMix.mix({
   require: './MyComponent',
   mocks: {
     SubComp1: './SubComp1',
     SubComp2: {
       require: './SubComp2',
       tagName: 'input'
     },
     SubComp3: {
       require: './SubComp3',
       // provide simple stub generator component for further flexibility
       mock: generateStub('INPUT')
     },
     SubComp4: {
       require: './SubComp4',
       mock: MyMockComponent
     },
     lodash: {
       require: 'lodash',
       // no need to provide react flag
       // if mock variable is provided, it will override default stub component (div)
       mock: function() { console.log('I am lodash mock') }
     }
   }
});

ES6 import statement support

In order to support, es6 module system, I have two possible interface in mind.

Import with option flag

var mix = MochaMix.mix({
  require: './MyComponent'
});

// equivalent to import MyComponent from './MyComponent';
var MyComponent = mix.import({ wildCard: true });

// equivalent to import * as MyWildCardComponent from './MyComponent';
var MyWildCardComponent = mix.import({ wildCard: true });

Separate import interface

var mix = MochaMix.mix({
  require: './MyComponent'
});

// equivalent to import MyComponent from './MyComponent';
var MyComponent = mix.import();

// equivalent to import * as MyWildCardComponent from './MyComponent';
var MyWildCardComponent = mix.importWildCard();

mockery's useCleanCache causes componentWillMount to error

mockery's useCleanCache option causes componentWillMount to cause
TypeError: Cannot read property '_currentElement' of null

Probably due to _currentElement being loaded using require statement.
Proposal:
disable useCleanCache from before
provide afterEach with mockery.resetCache()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.