summaryrefslogtreecommitdiff
path: root/web-ui/test/custom_matchers.js
blob: d68d21a8fea2b07e399ab96be938d6a39e13fd7f (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
define([], function() {
  'use strict';
  function toBeRenderedInMatcher () {
    return {
      compare: function (mail, node) {
        var result = {}, equals = {}, subject, tags, from, date, messages = [], notMessages = [];

        subject = node.find('#mail-' + mail.ident + ' .subject')[0];
        tags = _.map(node.find('#mail-' + mail.ident + ' .tags .tags-tag'), function (tag) { return tag.textContent; });
        date = node.find('#mail-' + mail.ident + ' .received-date');
        from = node.find('#mail-' + mail.ident + ' .from');

        if (subject && subject.textContent.trim() === mail.header.subject) {
          equals.subject = true;
          notMessages.push('not to be rendered with subject ' + mail.header.subject);
        } else {
          equals.subject = false;
          messages.push('to be rendered with subject ' + mail.header.subject + ', but was rendered with subject ' + subject.textContent.trim());
        }

        if (tags && tags.join(', ') === mail.tags.join(', ')) {
          equals.tags = true;
          notMessages.push('not to be rendered with tags ' + mail.tags.join(', '));
        } else {
          equals.tags = false;
          messages.push('to be rendered with tags ' + mail.tags.join(', ') + ', but was rendered with tags ' + tags.join(', '));
        }

        if (date && date.text().trim() === mail.header.date.split('T')[0]) {
          equals.date = true;
          notMessages.push('not to be rendered with date ' + mail.header.date.split('T')[0]);
        } else {
          equals.date = false;
          messages.push('to be rendered with date ' + mail.header.date.split('T')[0] + ', but was rendered with date ' + date.text().trim());
        }

        if (from && from.text().trim() === mail.header.from) {
          equals.from = true;
          notMessages.push('not to be rendered with from ' + mail.header.from);
        } else {
          equals.from = false;
          messages.push('to be rendered with from ' + mail.header.from + ', but was rendered with from ' + from.text().trim());
        }

        result.pass = equals.subject && equals.tags && equals.date && equals.from;

        if (result.pass) {
          result.message = 'Expected mail ' + mail.ident + ' ' + notMessages.join(', ');
        } else {
          result.message = 'Expected mail ' + mail.ident + ' ' + messages.join(', ');
        }

        return result;
      }
    };
  }

  function toBeRenderedSelectedInMatcher () {
    return {
      compare: function (mail, node) {
        var result = {}, toBeRendered, mailNode;

        toBeRendered = toBeRenderedInMatcher().compare(mail, node);

        mailNode = node.find('#mail-' + mail.ident);
        result.pass = toBeRendered.pass && mailNode.hasClass('selected');

        if (result.pass) {
          result.message = toBeRendered.message + '\nExpected mail ' + mail.ident + ' to not be selected in ' + mailNode.html();
        } else {
          result.message = toBeRendered.message + '\nExpected mail ' + mail.ident + ' to be selected in ' + mailNode.html();
        }

        return result;
      }
    };
  }

  return {
    toBeRenderedIn: toBeRenderedInMatcher,
    toBeRenderedSelectedIn: toBeRenderedSelectedInMatcher
  };
});