Skip to content

Commit a485531

Browse files
committed
Expand the TooltipView test suite (#447 #448)
1 parent f7015a7 commit a485531

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed
Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { enableI18n, event } from '../test-util';
22

3-
import { readit, rdfs, skos } from './../common-rdf/ns';
3+
import View from '../core/view';
4+
import { readit, rdfs, skos } from '../common-rdf/ns';
45
import { FlatLdObject } from '../common-rdf/json';
56
import Node from '../common-rdf/node';
67
import FlatItem from '../common-adapters/flat-item-model';
78

8-
import { Tooltip } from './tooltip-view';
9-
10-
function getDefaultItem(): FlatItem {
11-
return new FlatItem(new Node(getDefaultAttributes()));
12-
}
9+
import attachTooltip, { Tooltip } from './tooltip-view';
1310

1411
function getDefaultAttributes(): FlatLdObject {
1512
return {
@@ -19,25 +16,73 @@ function getDefaultAttributes(): FlatLdObject {
1916
{ '@value': 'Content' },
2017
],
2118
[skos.altLabel]: [
22-
{ '@value': 'alternativeLabel'}
19+
{ '@value': 'alternativeLabel' }
2320
],
2421
[skos.definition]: [
25-
{ '@value': 'This is a test definition'}
26-
]
22+
{ '@value': 'This is a test definition' }
23+
],
24+
[rdfs.comment]: [
25+
{ '@value': 'Also, I have a comment' }
26+
],
2727
}
2828
}
2929

30+
function getDefaultItem(): FlatItem {
31+
return new FlatItem(new Node(getDefaultAttributes()));
32+
}
33+
3034
describe('Tooltip', function () {
3135
beforeAll(enableI18n);
3236

33-
beforeEach( async function() {
37+
beforeEach(function() {
3438
this.item = getDefaultItem();
39+
this.substrate = new View();
40+
this.substrate.$el
41+
.html('<ul><li>A<li id=x>B<li>C</ul>')
42+
.appendTo(document.body);
43+
});
3544

45+
afterEach(function() {
46+
this.substrate.remove();
3647
});
3748

3849
it('includes the definition if it exists', async function () {
39-
let view = new Tooltip({ model: this.item });
50+
const view = new Tooltip({ model: this.item });
4051
await event(this.item, 'complete');
52+
expect(view.el).toHaveClass('tooltip');
4153
expect(view.$el.data('tooltip')).toEqual('This is a test definition');
4254
});
55+
56+
it('uses rdfs:comment otherwise', async function() {
57+
this.item.underlying.unset(skos.definition);
58+
const view = new Tooltip({ model: this.item });
59+
await event(this.item, 'complete');
60+
expect(view.el).toHaveClass('tooltip');
61+
expect(view.$el.data('tooltip')).toEqual('Also, I have a comment');
62+
});
63+
64+
it('disables itself when definition and comment are absent', async function() {
65+
this.item.underlying.unset(skos.definition);
66+
this.item.underlying.unset(rdfs.comment);
67+
const view = new Tooltip({ model: this.item });
68+
await event(this.item, 'complete');
69+
expect(view.$el.data('tooltip')).not.toBeDefined();
70+
expect(view.el).not.toHaveClass('tooltip');
71+
});
72+
73+
it('can be attached to another view', function() {
74+
const view = attachTooltip(this.substrate, {model: this.item});
75+
spyOn(view, 'remove').and.callThrough();
76+
expect(view.el).not.toHaveClass('is-tooltip-active');
77+
this.substrate.$el.trigger('mouseenter');
78+
expect(view.el).toHaveClass('is-tooltip-active');
79+
expect(view.$el.offset()).toEqual(this.substrate.$el.offset());
80+
expect(view.$el.width()).toEqual(this.substrate.$el.width());
81+
expect(view.$el.height()).toEqual(this.substrate.$el.height());
82+
this.substrate.$el.trigger('mouseleave');
83+
expect(view.el).not.toHaveClass('is-tooltip-active');
84+
expect(view.remove).not.toHaveBeenCalled();
85+
this.substrate.remove();
86+
expect(view.remove).toHaveBeenCalled();
87+
});
4388
});

0 commit comments

Comments
 (0)