summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/tags/ui/tag_shortcut.spec.js
blob: 862e0d1ca532cc6eade2be46718ca087caef436b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
describeComponent("tags/ui/tag_shortcut", function () {

  var parent, shortcut, component, TagShortcut;

  beforeEach(function () {
    TagShortcut = require('tags/ui/tag_shortcut');

    component = jasmine.createSpyObj('tagComponent', ['triggerSelect']);
    parent = $("<ul>");
    $('body').append(parent);
    shortcut = TagShortcut.appendedTo(parent, { linkTo: { name: 'inbox', counts: { total: 15 }}, trigger: component });
  });

  afterEach(function () {
    $('body')[0].removeChild(parent[0])
  });

  it('renders the shortcut inside the parent', function () {
    expect(parent.html()).toMatch('<a title="inbox">');
    expect(parent.html()).toMatch('<i class="fa fa-inbox"></i>');
    expect(parent.html()).toMatch('<div class="shortcut-label">inbox</div>');
  });

  it('selects and unselect on tag.select', function () {
    $(document).trigger(Pixelated.events.ui.tag.select, { tag: 'inbox'});

    expect(shortcut.$node).toHaveClass("selected");

    $(document).trigger(Pixelated.events.ui.tag.select, { tag: 'sent'});

    expect(shortcut.$node).not.toHaveClass("selected");
  });

  it('delegates the click to linked tag', function (){
    shortcut.$node.click();

    expect(component.triggerSelect).toHaveBeenCalled();
  });

  it('teardown shortcuts on event but only if they are not in the DOM', function () {
    parent.empty();
    var shortcutAddedAfterEmptyingParent = TagShortcut.appendedTo(parent, { linkTo: { name: 'inbox', counts: { total: 15 }}, trigger: component });
    // by now shorcut is not in the DOM anymore but shortcutAddedAfterEmptyingParent is

    spyOn(shortcut, 'teardown').andCallThrough();
    spyOn(shortcutAddedAfterEmptyingParent, 'teardown').andCallThrough();

    $(document).trigger(Pixelated.events.tags.shortcuts.teardown);

    expect(shortcut.teardown).toHaveBeenCalled();
    expect(shortcutAddedAfterEmptyingParent.teardown).not.toHaveBeenCalled();
  });
});