GithubHelp home page GithubHelp logo

rlugojr / test-fixture Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlearchive/test-fixture

0.0 2.0 0.0 18 KB

A simple DOM fixture helper for Web Component tests

JavaScript 10.56% HTML 89.44%

test-fixture's Introduction

Test Fixture

The <test-fixture> element can simplify the exercise of consistently reseting a test suite's DOM. To use it, wrap the test suite's DOM as a template:

<test-fixture id="SomeElementFixture">
  <template>
    <some-element id="SomeElementForTesting"></some-element>
  </template>
</test-fixture>

Now, the <test-fixture> element can be used to generate a copy if its template:

<script>
describe('<some-element>', function () {
  var someElement;

  beforeEach(function () {
    document.getElementById('SomeElementFixture').create();
    someElement = document.getElementById('SomeElementForTesting');
  });
});
</script>

Fixtured elements can be cleaned up by calling restore on the <test-fixture>:

  afterEach(function () {
    document.getElementById('SomeElementFixture').restore();
    // <some-element id='SomeElementForTesting'> has been removed
  });

<test-fixture> will create fixtures from all of its immediate <template> children. The DOM structure of fixture templates can be as simple or as complex as the situation calls for.

Even simpler usage in Mocha

In Mocha, usage can be simplified even further. Include test-fixture-mocha.js after Mocha in the <head> of your document and then fixture elements like so:

<script>
describe('<some-element>', function () {
  var someElement;

  beforeEach(function () {
    someElement = fixture('SomeElementFixture');
  });
});
</script>

Fixtured elements will be automatically restored in the afterEach phase of the current Mocha Suite.

Data-bound templates

Data-binding systems are also supported, as long as your (custom) template elements define a stamp(model) method that returns a document fragment. This allows you to stamp out templates w/ custom models for your fixtures.

For example, using Polymer 0.8's x-template:

<test-fixture id="bound">
  <template is="x-template">
    <span>{{greeting}}</span>
  </template>
</test-fixture>

You can pass an optional context argument to create() or fixture() to pass the model:

var bound = fixture('bound', {greeting: 'ohai thurr'});

The problem being addressed

Consider the following web-component-tester test suite:

<!doctype html>
<html>
<head>
  <title>some-element test suite</title>
  <!-- ... -->
  <link rel="import" href="../some-element.html">
</head>
<body>
  <some-element id="SomeElementForTesting"></some-element>
  <script>
describe('<some-element>', function () {
  var someElement;

  beforeEach(function () {
    someElement = document.getElementById('SomeElementForTesting');
  });

  it('can receive property `foo`', function () {
    someElement.foo = 'bar';
    expect(someElement.foo).to.be.equal('bar');
  });

  it('has a default `foo` value of `undefined`', function () {
    expect(someElement.foo).to.be.equal(undefined);
  });
});
  </script>
</body>
</html>

In this contrived example, the suite will pass or fail depending on which order the tests are run in. It is non-deterministic because someElement has internal state that is not properly reset at the end of each test.

It would be trivial in the above example to simply reset someElement.foo to the expected default value of undefined in an afterEach hook. However, for non-contrived test suites that target complex elements, this can result in a large quantity of ever-growing set-up and tear-down boilerplate.

test-fixture's People

Contributors

cdata avatar frankiefu avatar nevir avatar

Watchers

 avatar  avatar

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.