Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use assert.dom(selector).exists() instead of assert.dom.exists(selector) #7

Merged
merged 1 commit into from
Oct 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,60 +41,60 @@ Load `qunit-dom.js` *after* `qunit.js`:
Usage
------------------------------------------------------------------------------

Once installed the DOM element assertions are available at `assert.dom.*`:
Once installed the DOM element assertions are available at `assert.dom(...).*`:

```js
test('the title is friendly', function(assert) {
assert.dom.textContains('#title', 'Welcome');
assert.dom('#title').textContains('Welcome');
});
```


Assertions
------------------------------------------------------------------------------

### `exists(selector, [options], [message])`
### `missing(selector, [message])`
### `assert.dom(selector).exists([options], [message])`
### `assert.dom(selector).missing([message])`

Assert an [HTMLElement][] (or multiple) matching the `selector` exists.

```js
assert.dom.exists('#title');
assert.dom.exists('.choice', { count: 4 });
assert.dom.missing('.should-not-exist');
assert.dom('#title').exists();
assert.dom('.choice').exists({ count: 4 });
assert.dom('.should-not-exist').missing();
```


### `focused(selector|element, [message])`
### `notFocused(selector|element, [message])`
### `assert.dom(selector|element).focused([message])`
### `assert.dom(selector|element).notFocused([message])`

Assert that the [HTMLElement][] is or is not currently focused.

```js
assert.dom.focused('input.email');
assert.dom.notFocused(document.querySelector('input[type="password"]'));
assert.dom('input.email').focused();
assert.dom(document.querySelector('input[type="password"]')).notFocused();
```


### `textContains(selector|element, text, [message])`
### `assert.dom(selector|element).textContains(text, [message])`

Assert that the text of the [HTMLElement][] contains the given `text`, using
[`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent).

```js
assert.dom.textContains('#title', 'Welcome');
assert.dom.textContains(document.querySelector('#title'), 'Welcome');
assert.dom('#title').textContains('Welcome');
assert.dom(document.querySelector('#title')).textContains('Welcome');
```


### `textMatches(selector|element, regex, [message])`
### `assert.dom(selector|element).textMatches(regex, [message])`

Assert that the text of the [HTMLElement][] matches the given regular expression, using
[`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent).

```js
assert.dom.textMatches('.foo', /[12]\d{3}/);
assert.dom.textMatches(document.querySelector('.foo'), /[12]\d{3}/);
assert.dom('.foo').textMatches(/[12]\d{3}/);
assert.dom(document.querySelector('.foo')).textMatches(/[12]\d{3}/);
```


Expand Down
45 changes: 45 additions & 0 deletions lib/assertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import exists from './assertions/exists';
import missing from './assertions/missing';
import focused from './assertions/focused';
import notFocused from './assertions/not-focused';
import textContains from './assertions/text-contains';
import textMatches from './assertions/text-matches';

export default class DOMAssertions {
constructor(target, rootElement, testContext) {
this.target = target;
this.rootElement = rootElement;
this.testContext = testContext;
}

exists(options, message) {
exists.call(this, this.target, options, message);
}

missing(message) {
missing.call(this, this.target, message);
}

focused(message) {
focused.call(this, this.target, message);
}

notFocused(message) {
notFocused.call(this, this.target, message);
}

textContains(text, message) {
textContains.call(this, this.target, text, message);
}

textMatches(regex, message) {
textMatches.call(this, this.target, regex, message);
}

/**
* @private
*/
pushResult(result) {
this.testContext.pushResult(result);
}
}
6 changes: 3 additions & 3 deletions lib/qunit-dom.test.js → lib/assertions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

const fs = require('fs');

let bundleFile = fs.readFileSync(`${__dirname}/qunit-dom.js`, 'utf8');
let assertionsClassFile = fs.readFileSync(`${__dirname}/assertions.js`, 'utf8');

let assertions = fs.readdirSync(`${__dirname}/assertions`)
.filter(it => it.indexOf('.test.js') === -1);

assertions.forEach(filename => {
test(`${filename} is reexported`, () => {
expect(bundleFile).toContain(filename.replace(/\.js$/, ''));
test(`${filename} is imported`, () => {
expect(assertionsClassFile).toContain(filename.replace(/\.js$/, ''));
});
});
19 changes: 3 additions & 16 deletions lib/qunit-dom.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import * as QUnit from 'qunit';

import exists from './assertions/exists';
import missing from './assertions/missing';
import focused from './assertions/focused';
import notFocused from './assertions/not-focused';
import textContains from './assertions/text-contains';
import textMatches from './assertions/text-matches';
import DOMAssertions from './assertions';

QUnit.extend(QUnit.assert, {
dom: {
rootElement: document,
pushResult: QUnit.assert.pushResult,

exists,
missing,
focused,
notFocused,
textContains,
textMatches,
dom(target, rootElement) {
return new DOMAssertions(target, rootElement || document, this);
}
});
8 changes: 4 additions & 4 deletions tests/acceptance/qunit-dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ test('qunit-dom assertions are available', function(assert) {
assert.expect(5);

assert.ok(assert.dom, 'assert.dom is available');
assert.ok(assert.dom.textContains, 'assert.dom.textContains is available');
assert.ok(assert.dom('.foo').textContains, 'assert.dom(...).textContains is available');

visit('/');
andThen(() => {
assert.dom.exists('#title');
assert.dom.missing('#subtitle');
assert.dom.textContains('#title', 'Welcome');
assert.dom('#title').exists();
assert.dom('#subtitle').missing();
assert.dom('#title').textContains('Welcome');
});
});