Code Monkey home page Code Monkey logo

ko-component-tester's Introduction

ko-component-tester

NPM WTFPL Travis Coverage Status Dependency Status Peer Dependency Status NPM Downloads

TDD Helpers for Knockout components and bindings

Sample tests for a Knockout binding

'use strict'

const { renderHtml } = require('ko-component-tester')
const { expect } = require('chai')

describe ('Hello World text-binding', () => {
  let $el
  beforeEach(() => {
    $el = renderHtml({
      template: `<div data-bind="text: greeting"></div>`,
      viewModel: { greeting: 'Hello World'}
      })
  })
  it('renders', () => {
    expect($el).to.exist
  })
  it('renders correct text', () => {
    expect($el.html()).equals('Hello World')
  })
})

Sample tests for a Knockout component

'use strict'

const { renderComponent } = require('ko-component-tester')
const { expect } = require('chai')

describe('Hello World Component' , () => {
  let $el
  beforeEach(() => {
    $el = renderComponent({
      template: `<span data-bind="text: greeting"></span>`,
      viewModel: function() { this.greeting = 'Hello World' }
    })
  })
  afterEach(() => {
    $el.dispose()
  })
  it('renders', () => {
    expect($el).to.exist
  })
  it('renders correct content', () => {
    expect($el.html()).contains('Hello World')
  })
})

Sample Login test

'use strict'

const ko = require('knockout')
const { expect } = require('chai')
const sinon = require('sinon')
const { renderComponent } = require('../src')

class LoginComponent {
  constructor() {
    this.username = ko.observable()
    this.password = ko.observable()
  }
  submit() {}
}

describe('sample login component' , () => {
  let $el

  before(() => {
    $el = renderComponent({
      viewModel: LoginComponent,
      template: `
        <form data-bind="submit: submit">
          <input name="user" type="text" data-bind="value: username">
          <input name="pass" type="text" data-bind="value: password">
          <input type="submit">
        </form>`
    })
  })

  after(() => {
    $el.dispose()
  })

  it('renders correctly', () => {
    expect($el).to.exist
    expect($el.find('form'), 'contains a form').to.exist
    expect($el.find('input[name="user"]', 'contains a username field')).to.exist
    expect($el.find('input[name="pass"]', 'contains a password field')).to.exist
    expect($el.find('input[type="submit"]', 'contains a submit button')).to.exist
  })

  it('updates the viewmodel when a value is changed', () => {
    $el.find('input[name=user]').simulate('change', 'john')
    expect($el.$data().username()).equals('john')
  })

  it('can submit the form', () => {
    const submitSpy = sinon.spy($el.$data().submit)
    $el.find('input[name=user]').simulate('change', 'john')
    $el.find('input[name=pass]').simulate('change', 'p455w0rd')
    $el.find('input[type="submit"]').simulate('click')

    expect(submitSpy).to.be.called
  })
})

renderHtml(options)

returns a jQuery element containing the rendered html output

  • options.template - a string of html to be rendered
  • options.viewModel - an object, function, or class

Example with viewModel function:

const options = {
  template: `<div data-bind="text: greeting"></div>`,
  viewModel: function() { this.greeting = 'Hello Text Binding' }
}
const $el = renderHtml(options)

Example with viewModel class:

const options = {
  template: `<div data-bind="text: greeting"></div>`,
  viewModel: class ViewModel {
    constructor() {
      this.greeting = 'Hello Text Binding'
    }
  }
}
const $el = renderHtml(options)

Example with viewModel object:

const options = {
  template: `<div data-bind="text: greeting"></div>`,
  viewModel: { greeting: 'Hello Text Binding' }
}
const $el = renderHtml(options)

See spec for more examples of renderHtml().

renderComponent(component, params, bindingContext)

returns a jQuery element containing the rendered html output

  • component.template - a string of html to be rendered
  • component.viewModel - a function, class, or instance
  • params - optional params to be passed into the viewModel's constructor
  • bindingContext - optional bindingContext to inject (useful for stubbing $parent or $index)

Example with viewModel function:

const component = {
  template: `<div data-bind="text: greeting"></div>`,
  viewModel: function() { this.greeting = 'Hello Text Binding' }
}
const $el = renderComponent(component)
// $el.dispose()

Example with viewModel class:

const component = {
  template: `<div data-bind="text: greeting"></div>`,
  viewModel: class ViewModel {
    constructor(params) {
      this.greeting = params.greeting
    }
  }
}
const params = {
  greeting: 'Hello Text Binding'
}
const $el = renderComponent(component, params)
// $el.dispose()

Example with viewModel instance:

class ViewModel {
  constructor(params) {
    this.greeting = params.greeting
  }
}
const component = {
  template: `<div data-bind="text: greeting"></div>`,
  viewModel: { instance: new ViewModel(params) }
}
const $el = renderComponent(component)
// $el.dispose()

See spec for more examples of renderComponent().

$el.getComponentParams()

see spec for examples

$el.waitForBinding()

see spec for examples

$el.waitForProperty()

see spec for examples

$el.simulate(event, value)

  • event - the event to simulate, eg 'click', or 'change'
  • value - if provided this value will be assigned. It's handy for assigning a value to a textbox and triggering a change event like this.
// simulate changing the value of a textbox
$input.simulate('change', 'new value')
// simulate clicking a button
$submit.simulate('click')

Attribution

https://github.com/jeremija/kotest

ko-component-tester's People

Contributors

barake avatar barsh avatar caseywebb avatar greenkeeperio-bot avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

ko-component-tester's Issues

Shallow render components

One of the nice things about testing React components with Enzyme is that you can test your components as shallow. In this case, only the top level component gets rendered and all child components are ignored. This makes a lot of sense for unit testing, since you want to test your components as small, independent parts.

It would be nice if you could just call renderShallowComponent(...), but I'm guessing that would also be really involved. I can't say I have any idea how this would be implemented, maybe override the component binding somehow?

A simpler method would be an option to ignore certain components. So if you have a TodoList you could do:

renderComponent({
    viewModel: TodoList,
    template: todoListTemplate,
    ignoreComponents: ['Todo']
})

These sub components would be registered as empty components. This is what I currently do in a before-call, but it would be tidier to have the functionality built in.

Thoughts?

Basic examples are responding with errors

This file in /test

'use strict'
const { renderHtml } = require('ko-component-tester')

with the following package.json

{
  "name": "knockout-tests",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jquery": "^2.2.0",
    "knockout": "^3.4.2",
    "ko-component-tester": "^3.2.1"
  }
}

gives errors

> [email protected] test /Users/sebs/projects/customers/sumcumo/knockout-tests
> mocha

/Users/sebs/projects/..../knockout-tests/node_modules/ko-component-tester/dist/ko-component-tester.js:67
	$.fn.simulate = function (eventName, value) {
	              ^

TypeError: Cannot set property 'simulate' of undefined
    at Object.module.exports (....
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

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.