All Projects → MithrilJS → Mithril Query

MithrilJS / Mithril Query

Licence: mit
Query mithril virtual dom for testing purposes

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Mithril Query

Iconswitch
🍭 Custom Android Switch widget
Stars: ✭ 874 (+732.38%)
Mutual labels:  selector
Reapex
A lightweight framework to build pluggable and extendable redux/react application
Stars: ✭ 58 (-44.76%)
Mutual labels:  selector
Xselectorutil
一个可以用代码处理控件的阴影效果,及用代码在TextView、EditText、Button等控件设置selector背景(触摸反馈,样式变化、文字颜色变化、hint文字颜色变化等效果)的组件
Stars: ✭ 98 (-6.67%)
Mutual labels:  selector
Bell
⏱ Counting down to the next time the bell rings at school
Stars: ✭ 15 (-85.71%)
Mutual labels:  mithril
React Propers
Select react doms , and update props.
Stars: ✭ 40 (-61.9%)
Mutual labels:  selector
Postcss Prefix Selector
Prefix all CSS rules with a selector
Stars: ✭ 75 (-28.57%)
Mutual labels:  selector
Mobile Select
手机移动端选择组件 支持是否级联/单选到多选/可异步更新数据等..
Stars: ✭ 829 (+689.52%)
Mutual labels:  selector
Pd Select
vue components ,like ios 3D picker style,vue 3d 选择器组件,3D滚轮
Stars: ✭ 101 (-3.81%)
Mutual labels:  selector
Lichobile
lichess.org mobile application
Stars: ✭ 1,043 (+893.33%)
Mutual labels:  mithril
Resq
React Element Selector Query (RESQ) - Query React components and children by component name or HTML selector
Stars: ✭ 89 (-15.24%)
Mutual labels:  selector
Redux Tide
Simple library for redux crud normalized state and actions/selectors for it
Stars: ✭ 20 (-80.95%)
Mutual labels:  selector
Layui dropdown
基于layui框架的下拉控件,支持菜单下拉,自定义下拉内容,兼容表格。
Stars: ✭ 40 (-61.9%)
Mutual labels:  selector
Mithril Infinite
Infinite scroll for Mithril
Stars: ✭ 83 (-20.95%)
Mutual labels:  mithril
Todomvc Mithril
TodoMVC app using Mithril.js with CoffeeScript and Brunch
Stars: ✭ 15 (-85.71%)
Mutual labels:  mithril
Lambda Converters
Strongly-typed lambda expressions as value converters, data template selectors, and validation rules
Stars: ✭ 99 (-5.71%)
Mutual labels:  selector
Mithril Data
A rich data model library for Mithril javascript framework
Stars: ✭ 17 (-83.81%)
Mutual labels:  mithril
React Native Modal Dropdown
A react-native dropdown/picker/selector component for both Android & iOS.
Stars: ✭ 1,103 (+950.48%)
Mutual labels:  selector
Dropdown
a lightweight dropdown of jQuery plugins
Stars: ✭ 105 (+0%)
Mutual labels:  selector
Customalertviewdialogue
Custom AlertView Dialogue is the world's most advanced alert view library. Custom AlertView Dialogue includes simple message popups, confirmation alerts, selector popups, action sheet bottom menus, and input/feedback contact forms.
Stars: ✭ 100 (-4.76%)
Mutual labels:  selector
Statebutton
一个可以用代码设置selector背景(按下去背景颜色更改,样式变化等等)的button, 再也不用写selector了
Stars: ✭ 1,276 (+1115.24%)
Mutual labels:  selector

mithril-query

Gitter Build Status rethink.js js-standard-style

Query mithril virtual dom for testing purposes

Installation

npm install mithril-query --save-dev

Setup

In order to run tests in mithril 2.x we need to do some setup. That is to mock the dom for the mithril render and request modules. This can be done by requiring a 'setup' file in your 'mocha' tests with the following contents.

global.window = Object.assign(
  require('mithril/test-utils/domMock.js')(),
  require('mithril/test-utils/pushStateMock')()
)
global.requestAnimationFrame = callback =>
  global.setTimeout(callback, 1000 / 60)

Changes from version 3.x to 4.x

Root state access

... is gone, since mithril does not provide a way to access it

Booleans

... are now rendered as empty strings, like mithril does, because, well, mithril renders

Lifecycles

... are now fully supported, including synthetic DOM elements 🎉

find/first

... are now returning DOM elements instead of vdom nodes.

Custom events

... aren't supported anymore. Feel free to file a ticket, if you want them back.

Usage

You can run this tests server side or use browserify and run them in browsers.

const m = require('mithril')

module.exports = {
  view: function() {
    return m('div', [
      m('span', 'spanContent'),
      m('#fooId', 'fooContent'),
      m('.barClass', 'barContent'),
    ])
  },
}
/* eslint-env mocha */
global.window = Object.assign(
  require('mithril/test-utils/domMock.js')(),
  require('mithril/test-utils/pushStateMock')()
)
global.requestAnimationFrame = callback =>
  global.setTimeout(callback, 1000 / 60)

const simpleModule = require('./simple')
const mq = require('../')

describe('simple module', function() {
  it('should generate appropriate output', function() {
    var output = mq(simpleModule)
    output.should.have('span')
    output.should.have('div > span')
    output.should.have('#fooId')
    output.should.have('.barClass')
    output.should.have(':contains(barContent)')
    output.should.contain('barContent')
  })
})

Run the test with

mocha simple.test.js

API

Initialise

First call mithril-query with either a vnode or a component. You can call it with one extra argument which will be used as attrs in the component case.

var mq = require('mithril-query')

// plain vnode
var out = mq(m('div'))

// object component
var myComponent = {
  view: function ({ attrs }) {
    return m('div', attrs.text)
  }
}
var out = mq(myComponent, { text: 'huhu' })

// closure component
function myComponent() {
  return {
    view: function ({ attrs }) {
      return m('div', attrs.text)
    }
  }
}
var out = mq(myComponent, { text: 'huhu' })

Query API

As you can see mq returns an out-Object which has the following test-API.

  • out.first(selector) – Returns the first element that matches the selector (think document.querySelector).
  • out.find(selector) – Returns all elements that match the selector (think document.querySelectorAll).
  • out.has(selector) –  Returns true if any element in tree matches the selector, otherwise false.
  • out.contains(string) – Returns true if any element in tree contains the string, otherwise false.
  • out.log(selector, [logFN]) – Small helper function to log out what was selected. Mainly for debugging purposes. You can give an optional function which is called with the result. It defaults to HTML-Pretty-Printer (pretty-html-log] that logs the HTML-representation to stdout.

You can use these nice assertions. They throw errors if they're not fulfilled. See the example in the example folder.

  • out.should.have([count], selector)

Throws if no element is found with selector. If count is given, it throws if count does not match.

  • out.should.not.have(selector) – Throws if an element is found with selector.
  • out.should.have.at.least(count, selector) – Throws if there a fewer than count elements matching the selector
  • out.should.have([selector0, selector1, selector2]) – Throws there aren't at least one element for each selector.
  • out.should.contain(string) – Throws if no element contains string.
  • out.should.not.contain(string) - Throws if any element contains string.

Event triggering

It is also possible to trigger element events like onfocus and onclick and set values on <input>-fields. This allows you to write "integration tests" that run also on server side.

Attention: Currently there is no event bubbling supported.

  • out.click(selector, [eventData]) – Runs onclick for first element that matches selector. Optional eventData is given as to the event constructor. eventData.redraw = false is respected.
  • out.setValue(selector, string, [eventData]) – Runs oninput and onchange for first element that matches selector.
  • out.trigger(selector, eventname, [eventData]) – General purpose event triggerer. Calls eventname on first matching element.

It also supports key events

  • out.keydown(selector, keycode, [eventData]) – calls onkeydown with keycode
  • out.keydown(selector, keyname, [eventData]) – calls onkeydown with keycode mapped from name. Mapping is done with this lib.

keyup, keypress are supported as well.

Auto "Redrawing"

Since mithril-query uses mithril on a fake DOM, auto rendering works as expected.

Example:

  // module code
  const component = {
    visible: true
    oninit({ state }) {
      state.toggleMe = () => (state.visible = !state.visible)
    },
    view({ state }) {
      return m(
        state.visible ? '.visible' : '.hidden',
        { onclick: state.toggleMe},
        'Test'
      )
    },
  }


  // actual test
  out = mq(component)
  out.should.have('.visible')
  out.click('.visible')
  out.should.not.have('.visible')
  out.should.have('.hidden')
  out.click('.hidden', { redraw: false })
  out.should.have('.hidden')

As you can see, you can prevent auto redraw by providing a redraw: false as last argument to click method.

You can also manually trigger redraw:

var out = mq(module)
out.should.have('.visible')
out.redraw()

helpers

If you need to access the rendered root element you can simply access it with

out.rootEl

onremove handling

To trigger onremove-handlers of all initialized components, just call out.onremove()

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].