2019-07-26 07:38:36 +00:00
|
|
|
import { find, settled } from '@ember/test-helpers';
|
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
|
2019-07-26 07:38:36 +00:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import RSVP from 'rsvp';
|
2021-03-27 00:02:54 +00:00
|
|
|
import { formatBytes } from 'nomad-ui/utils/units';
|
2019-07-26 07:38:36 +00:00
|
|
|
|
|
|
|
module('Integration | Component | image file', function(hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
|
|
|
const commonTemplate = hbs`
|
2020-06-01 19:03:56 +00:00
|
|
|
<ImageFile @src={{src}} @alt={{alt}} @size={{size}} />
|
2019-07-26 07:38:36 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const commonProperties = {
|
2019-08-07 22:08:38 +00:00
|
|
|
src: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
|
2019-07-26 07:38:36 +00:00
|
|
|
alt: 'This is the alt text',
|
|
|
|
size: 123456,
|
|
|
|
};
|
|
|
|
|
|
|
|
test('component displays the image', async function(assert) {
|
|
|
|
this.setProperties(commonProperties);
|
|
|
|
|
|
|
|
await this.render(commonTemplate);
|
|
|
|
|
|
|
|
assert.ok(find('img'), 'Image is in the DOM');
|
|
|
|
assert.equal(
|
|
|
|
find('img').getAttribute('src'),
|
|
|
|
commonProperties.src,
|
|
|
|
`src is ${commonProperties.src}`
|
|
|
|
);
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2019-07-26 07:38:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('the image is wrapped in an anchor that links directly to the image', async function(assert) {
|
|
|
|
this.setProperties(commonProperties);
|
|
|
|
|
|
|
|
await this.render(commonTemplate);
|
|
|
|
|
|
|
|
assert.ok(find('a'), 'Anchor');
|
|
|
|
assert.ok(find('a > img'), 'Image in anchor');
|
|
|
|
assert.equal(
|
|
|
|
find('a').getAttribute('href'),
|
|
|
|
commonProperties.src,
|
|
|
|
`href is ${commonProperties.src}`
|
|
|
|
);
|
|
|
|
assert.equal(find('a').getAttribute('target'), '_blank', 'Anchor opens to a new tab');
|
|
|
|
assert.equal(
|
|
|
|
find('a').getAttribute('rel'),
|
|
|
|
'noopener noreferrer',
|
|
|
|
'Anchor rel correctly bars openers and referrers'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('component updates image meta when the image loads', async function(assert) {
|
|
|
|
const { spy, wrapper, notifier } = notifyingSpy();
|
|
|
|
|
|
|
|
this.setProperties(commonProperties);
|
|
|
|
this.set('spy', wrapper);
|
|
|
|
|
|
|
|
this.render(hbs`
|
2020-06-01 19:03:56 +00:00
|
|
|
<ImageFile @src={{src}} @alt={{alt}} @size={{size}} @updateImageMeta={{spy}} />
|
2019-07-26 07:38:36 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
await notifier;
|
|
|
|
assert.ok(spy.calledOnce);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('component shows the width, height, and size of the image', async function(assert) {
|
|
|
|
this.setProperties(commonProperties);
|
|
|
|
|
|
|
|
await this.render(commonTemplate);
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
const statsEl = find('[data-test-file-stats]');
|
|
|
|
assert.ok(
|
|
|
|
/\d+px \u00d7 \d+px/.test(statsEl.textContent),
|
|
|
|
'Width and height are formatted correctly'
|
|
|
|
);
|
|
|
|
assert.ok(
|
2021-03-27 00:02:54 +00:00
|
|
|
statsEl.textContent.trim().endsWith(formatBytes(commonProperties.size) + ')'),
|
2019-07-26 07:38:36 +00:00
|
|
|
'Human-formatted size is included'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function notifyingSpy() {
|
|
|
|
// The notifier must resolve when the spy wrapper is called
|
|
|
|
let dispatch;
|
|
|
|
const notifier = new RSVP.Promise(resolve => {
|
|
|
|
dispatch = resolve;
|
|
|
|
});
|
|
|
|
|
|
|
|
const spy = sinon.spy();
|
|
|
|
|
|
|
|
// The spy wrapper must call the spy, passing all arguments through, and it must
|
|
|
|
// call dispatch in order to resolve the promise.
|
|
|
|
const wrapper = (...args) => {
|
|
|
|
spy(...args);
|
|
|
|
dispatch();
|
|
|
|
};
|
|
|
|
|
|
|
|
// All three pieces are required to wire up a component, pause test execution, and
|
|
|
|
// write assertions.
|
|
|
|
return { spy, wrapper, notifier };
|
|
|
|
}
|