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

Spike on assertContent test helper #146

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ This addon exposes testing helpers which can be used inside of the consuming app
* `assertTooltipNotRendered(assert)`: asserts if the tooltip has not been rendered. When enableLazyRendering is true the tooltip will only be rendered after the user has interacted with the $target element.
* `assertTooltipSide(assert, { side: 'right' }): asserts that the tooltip is shown on the correct side of the target. Additional options that can be passed are `selector` and `targetSelector`.
* `assertTooltipSpacing(assert, { side: 'right', spacing: 10 }): asserts that the tooltip is a given distance from the target (on a given side). `side` and `spacing` must be passed. Additional options that can be passed are `selector` and `targetSelector`.
* `assertContent(assert, contentString): asserts that the content of the tooltip matches a given string.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good enough of a description for now. I'm going to be revamping the test helper documentation (#144) to give each test helpers its own heading and section in the readme.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be updated...

assertTooltipContent(assert, { contentString: 'hello' }); .....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

* `triggerTooltipTargetEvent($targetElement, eventName)`: triggers an event on the passed element. The event will be triggered within an Ember.run so that the tooltip's asynchronicity is accounted for. eventName can be mouseenter, mouseleave, click, focus, focusin, and blur.

Each test helper also accepts an `options` object as a final parameter. If a `selector` property is provided the assertions and actions will be run against the single element found from that selector.
Expand Down
17 changes: 17 additions & 0 deletions test-support/helpers/ember-tooltips.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,20 @@ export function assertTooltipSpacing(assert, options) {
- Tooltip should be on the ${side} side of the target: ${isSideCorrect}.
- On the ${side} side of the target, the tooltip should be ${spacing}px from the target but it was ${actualSpacing}px`);
}

export function assertTooltipContent(assert, options = {}) {
const { contentString } = options;

if (contentString === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use isEmpty then a user would not be able to test the case where contentString is an empty string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I meant 'isNone'

Ember.assert('You must specify a contentString property in the options parameter');
}

const $tooltip = getTooltipFromBody(options.selector);
const tooltipContent = $tooltip.text().trim();

assert.equal(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not blocking, could you follow the syntax we did elsewhere in this file...

assert.equal(a, b, 'some description');

PS: I like that you added the tooltipContent and contentString in the description

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

tooltipContent,
contentString,
`Content of tooltip (${tooltipContent}) matched expected (${contentString})`
);
}
68 changes: 68 additions & 0 deletions tests/integration/components/content-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { assertTooltipContent } from '../../helpers/ember-tooltips';

moduleForComponent('tooltip-on-element', 'Integration | Option | content', {
integration: true,
});

test('assertTooltipContent correctly matches expected tootltip content', function(assert) {

assert.expect(2);

this.render(hbs`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the div is unnecessary. Could be written like...

this.render(hbs`{{tooltip-on-element test="foo"}}`);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes the tests harder to read, but would be happy to change. My reasoning is that the surrounding div gives someone reading the test more context.

Again, happy to change, please just confirm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

@sir-dunxalot sir-dunxalot Jan 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To stay consistent with our other tests I think we should remove the <div> and use:

this.render(hbs`{{tooltip-on-element test="foo"}}`);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got it. Updating now.

<div>
:)
{{tooltip-on-element text='Smiley face'}}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good addition to this test will be a test for the block form:

<div>
  :)
  {{#tooltip-on-element}}
    Smiley face
  {{/tooltip-on-element}}
</div>

</div>
`);

assertTooltipContent(assert, {
contentString: 'Smiley face',
});

this.render(hbs`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be simplified too (no div)

<div>
:(
{{#tooltip-on-element}}
Frowning face
{{/tooltip-on-element}}
</div>
`);

assertTooltipContent(assert, {
contentString: 'Frowning face',
});
});

test('assertTooltipContent correctly compares expected and discovered tooltip content of tooltip', function(assert) {

assert.expect(2);

this.render(hbs`
<div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everywhere

:)
{{tooltip-on-element text='Smiley face'}}
</div>
`);

const stubbedAssert = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain what stubbedAssert does? I'm confused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just stubs the equal on assert.equal that is passed in to our helper. The value this adds is testing that our helper calls what we think it will with the values it should have.

equal(arg1, arg2/* , msg */) {
assert.equal(
arg1,
'Smiley face',
'Helper correctly finds actual content of tooltip'
);

assert.equal(
arg2,
'Frowning face',
'Helper correctly intends to compare to string we provide'
);
},
};

assertTooltipContent(stubbedAssert, {
contentString: 'Frowning face',
});
});
12 changes: 10 additions & 2 deletions tests/integration/components/popover/popover-on-component-test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { assertTooltipNotRendered, assertTooltipRendered } from '../../../helpers/ember-tooltips';
import {
assertTooltipNotRendered,
assertTooltipRendered,
assertTooltipContent,
} from '../../../helpers/ember-tooltips';

moduleForComponent('popover-on-component', 'Integration | Component | popover on component', {
integration: true,
});

test('popover-on-component does render when enableLazyRendering=false', function(assert) {

assert.expect(1);
assert.expect(2);

this.render(hbs`
{{#some-component}}
Expand All @@ -18,6 +22,10 @@ test('popover-on-component does render when enableLazyRendering=false', function
{{/some-component}}
`);

assertTooltipContent(assert, {
contentString: 'template block text',
});

assertTooltipRendered(assert);
});

Expand Down
8 changes: 4 additions & 4 deletions tests/integration/components/text-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { assertTooltipRendered } from '../../helpers/ember-tooltips';
import { assertTooltipRendered, assertTooltipContent } from '../../helpers/ember-tooltips';

moduleForComponent('tooltip-on-element', 'Integration | Component | inline', {
integration: true,
Expand All @@ -14,9 +14,9 @@ test('tooltip-on-element renders with text param', function(assert) {
{{tooltip-on-element text='Here is more info'}}
`);

assert.equal(this.$().text().trim(), 'Here is more info',
'Should render with content equal to the text property');
assertTooltipContent(assert, {
contentString: 'Here is more info',
});

assertTooltipRendered(assert);

});
15 changes: 13 additions & 2 deletions tests/integration/components/tooltip-on-component-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { assertTooltipNotRendered, assertTooltipRendered } from '../../helpers/ember-tooltips';
import {
assertTooltipNotRendered,
assertTooltipRendered,
assertTooltipContent,
} from '../../helpers/ember-tooltips';

moduleForComponent('tooltip-on-component', 'Integration | Component | tooltip-on-component', {
integration: true,
});

test('tooltip-on-component does render when enableLazyRendering=false', function(assert) {

assert.expect(2);

this.render(hbs`
{{#some-component}}
{{#tooltip-on-component enableLazyRendering=false}}
Expand All @@ -16,12 +22,17 @@ test('tooltip-on-component does render when enableLazyRendering=false', function
{{/some-component}}
`);

assertTooltipRendered(assert);
assertTooltipContent(assert, {
contentString: 'template block text',
});

assertTooltipRendered(assert);
});

test('tooltip-on-component does not eagerly render when enableLazyRendering=true', function(assert) {

assert.expect(1);

this.render(hbs`
{{#some-component}}
{{#tooltip-on-component enableLazyRendering=true}}
Expand Down
13 changes: 9 additions & 4 deletions tests/integration/components/tooltip-on-element-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { moduleForComponent, test } from 'ember-qunit';
import { assertTooltipRendered } from '../../helpers/ember-tooltips';
import hbs from 'htmlbars-inline-precompile';

import { assertTooltipContent } from '../../helpers/ember-tooltips';

moduleForComponent('tooltip-on-element', 'Integration | Component | tooltip on element', {
integration: true,
});
Expand All @@ -16,11 +18,11 @@ test('tooltip-on-element renders', function(assert) {
{{/tooltip-on-element}}
`);

assert.equal(this.$().text().trim(), 'template block text',
'Should render with content');
assertTooltipContent(assert, {
contentString: 'template block text',
});

assertTooltipRendered(assert);

});

test('tooltip-on-element has the proper aria-describedby tag', function(assert) {
Expand All @@ -40,7 +42,10 @@ test('tooltip-on-element has the proper aria-describedby tag', function(assert)
const $tooltipTarget = this.$('.target');
const describedBy = $tooltipTarget.attr('aria-describedby');

assert.equal(this.$(`#${describedBy}`).text().trim(), 'Some info in a tooltip.');
assertTooltipContent(assert, {
selector: `#${describedBy}`,
contentString: 'Some info in a tooltip.',
});
assert.equal(describedBy.indexOf('#'), '-1');

});
19 changes: 13 additions & 6 deletions tests/integration/components/update-for-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import Ember from 'ember';
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

const { RSVP, run } = Ember;
const {
RSVP,
run,
} = Ember;

import { assertTooltipContent } from '../../helpers/ember-tooltips';

moduleForComponent('tooltip-on-element', 'Integration | Option | updateFor', {
integration: true,
Expand Down Expand Up @@ -34,14 +39,16 @@ test('updateFor test', function(assert) {
`);

const done = assert.async();
const $tooltip = this.$();

assert.equal($tooltip.text().trim(), '...',
'Should render ...');
assertTooltipContent(assert, {
contentString: '...',
});

run.later(() => {
assert.equal($tooltip.text().trim(), 'Some model',
'Should render "Some model"');
assertTooltipContent(assert, {
contentString: 'Some model',
});

done();
}, 200);

Expand Down