Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrinko committed Apr 28, 2020
1 parent f3d4e6d commit 1f5f9ee
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
/.node_modules.ember-try/
/bower.json.ember-try
/package.json.ember-try

# backstop files
/ember-backstop
18 changes: 13 additions & 5 deletions addon-test-support/backstop.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ function copyAttributesToBodyCopy(bodyCopy, testingContainer) {
Object.keys(attributesToCopy).forEach(key => copyAttr(attributesToCopy[key]));
}

function prepareInputValuesForCopying(snapshotRoot) {
/**
* Sync JS properties to HTML attributes, so Backstop can see it
* @param snapshotRoot
*/
export function prepareInputValuesForCopying(snapshotRoot) {
snapshotRoot.querySelectorAll('input')
.forEach(function (item) {
item.setAttribute('value', item.value);
if (item.checked) {
item.setAttribute('checked', 'checked');
} else {
item.removeAttribute('checked');
if (item.type === 'radio' || item.type === 'checkbox') {
if (item.checked) {
item.setAttribute('checked', 'checked');
} else {
item.removeAttribute('checked');
}
// note: can't handle `indeterminate` state, as there
// is no such HTML attribute
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion tests/acceptance/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import backstop from 'ember-backstop/test-support/backstop';
module('Acceptance | list rentals', function(hooks) {
setupApplicationTest(hooks);

test('Simple tests.', async function(assert) {
test('Should load the app and should match previously approved reference.', async function(assert) {
await visit('/');
await backstop(assert);
assert.equal(currentURL(), '/', 'URL should be at root.');
assert.dom(this.element.querySelector('#title')).hasText('Welcome to Ember');
});

});
57 changes: 57 additions & 0 deletions tests/unit/addon-test-support/backstop-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { prepareInputValuesForCopying } from 'ember-backstop/test-support/backstop';

module('Unit | Addon Test Support', hooks => {
setupTest(hooks);

module('#prepareInputValuesForCopying tests', () => {

test('Should copy JS DOM attributes into snapshot DOM attribute', async function(assert) {

// Given DOM node with HTML inputs without
// default attribute values provided
var snapshot = document.createElement('form');
snapshot.innerHTML = `
<input type="radio" id="checkbox1" />
<input type="radio" id="radio1" />
<input type="checkbox" id="checkbox2"/>
<input type="checkbox" id="radio2"/>
<input type="text" id="text1"/>
<input type="button" id="button1"/>
`;

// Given `value` and `checked` attributes are
// set programmatically as JS props
snapshot['radio1'].value = 'radio value 1';
snapshot['radio1'].checked = true;

snapshot['checkbox1'].value = 'checkbox value 1';
snapshot['checkbox1'].checked = true;

snapshot['radio2'].value = 'radio value 2';
snapshot['checkbox2'].value = 'checkbox value 2';

snapshot['text1'].value = 'text value';
snapshot['button1'].value = 'button value';
snapshot['button1'].checked = true; // unsupported "checked" attribute for a button

// When function is called with given snapshot
prepareInputValuesForCopying(snapshot);

assert.equal(snapshot['radio1'].getAttribute('value'), 'radio value 1');
assert.equal(snapshot['radio1'].hasAttribute('checked'), true);

assert.equal(snapshot['checkbox1'].getAttribute('value'), 'checkbox value 1');
assert.equal(snapshot['checkbox1'].hasAttribute('checked'), true);

assert.equal(snapshot['radio2'].getAttribute('value'), 'radio value 2');
assert.equal(snapshot['checkbox2'].getAttribute('value'), 'checkbox value 2');
assert.equal(snapshot['text1'].getAttribute('value'), 'text value');
assert.equal(snapshot['button1'].getAttribute('value'), 'button value');
assert.equal(snapshot['button1'].hasAttribute('checked'), false);
});

});

});

0 comments on commit 1f5f9ee

Please sign in to comment.