issue_card_spec.js 7.24 KB
Newer Older
1
/* global ListAssignee */
Phil Hughes's avatar
Phil Hughes committed
2 3 4
/* global ListLabel */
/* global ListIssue */

5 6
import Vue from 'vue';

7 8 9 10 11 12
import '~/boards/models/issue';
import '~/boards/models/label';
import '~/boards/models/list';
import '~/boards/models/assignee';
import '~/boards/stores/boards_store';
import '~/boards/components/issue_card_inner';
Eric Eastwood's avatar
Eric Eastwood committed
13
import { listObj } from './mock_data';
Phil Hughes's avatar
Phil Hughes committed
14 15

describe('Issue card component', () => {
16
  const user = new ListAssignee({
Phil Hughes's avatar
Phil Hughes committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    id: 1,
    name: 'testing 123',
    username: 'test',
    avatar: 'test_image',
  });
  const label1 = new ListLabel({
    id: 3,
    title: 'testing 123',
    color: 'blue',
    text_color: 'white',
    description: 'test',
  });
  let component;
  let issue;
  let list;

  beforeEach(() => {
    setFixtures('<div class="test-container"></div>');

    list = listObj;
    issue = new ListIssue({
      title: 'Testing',
Simon Knox's avatar
Simon Knox committed
39
      id: 1,
Phil Hughes's avatar
Phil Hughes committed
40 41 42
      iid: 1,
      confidential: false,
      labels: [list.label],
43
      assignees: [],
Phil Hughes's avatar
Phil Hughes committed
44 45 46 47
    });

    component = new Vue({
      el: document.querySelector('.test-container'),
48 49 50
      components: {
        'issue-card': gl.issueBoards.IssueCardInner,
      },
Phil Hughes's avatar
Phil Hughes committed
51 52 53 54 55
      data() {
        return {
          list,
          issue,
          issueLinkBase: '/test',
Phil Hughes's avatar
Phil Hughes committed
56
          rootPath: '/',
Phil Hughes's avatar
Phil Hughes committed
57 58 59 60 61 62
        };
      },
      template: `
        <issue-card
          :issue="issue"
          :list="list"
Phil Hughes's avatar
Phil Hughes committed
63 64
          :issue-link-base="issueLinkBase"
          :root-path="rootPath"></issue-card>
Phil Hughes's avatar
Phil Hughes committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      `,
    });
  });

  it('renders issue title', () => {
    expect(
      component.$el.querySelector('.card-title').textContent,
    ).toContain(issue.title);
  });

  it('includes issue base in link', () => {
    expect(
      component.$el.querySelector('.card-title a').getAttribute('href'),
    ).toContain('/test');
  });

  it('includes issue title on link', () => {
    expect(
      component.$el.querySelector('.card-title a').getAttribute('title'),
    ).toBe(issue.title);
  });

  it('does not render confidential icon', () => {
    expect(
      component.$el.querySelector('.fa-eye-flash'),
    ).toBeNull();
  });

  it('renders confidential icon', (done) => {
    component.issue.confidential = true;

96
    Vue.nextTick(() => {
Phil Hughes's avatar
Phil Hughes committed
97
      expect(
Phil Hughes's avatar
Phil Hughes committed
98
        component.$el.querySelector('.confidential-icon'),
Phil Hughes's avatar
Phil Hughes committed
99 100
      ).not.toBeNull();
      done();
101
    });
Phil Hughes's avatar
Phil Hughes committed
102 103 104 105 106 107 108 109 110 111 112
  });

  it('renders issue ID with #', () => {
    expect(
      component.$el.querySelector('.card-number').textContent,
    ).toContain(`#${issue.id}`);
  });

  describe('assignee', () => {
    it('does not render assignee', () => {
      expect(
113
        component.$el.querySelector('.card-assignee .avatar'),
Phil Hughes's avatar
Phil Hughes committed
114 115 116 117 118
      ).toBeNull();
    });

    describe('exists', () => {
      beforeEach((done) => {
119
        component.issue.assignees = [user];
Phil Hughes's avatar
Phil Hughes committed
120

121
        Vue.nextTick(() => done());
Phil Hughes's avatar
Phil Hughes committed
122 123 124 125
      });

      it('renders assignee', () => {
        expect(
126
          component.$el.querySelector('.card-assignee .avatar'),
Phil Hughes's avatar
Phil Hughes committed
127 128 129 130 131
        ).not.toBeNull();
      });

      it('sets title', () => {
        expect(
132
          component.$el.querySelector('.card-assignee img').getAttribute('data-original-title'),
Phil Hughes's avatar
Phil Hughes committed
133 134 135 136 137
        ).toContain(`Assigned to ${user.name}`);
      });

      it('sets users path', () => {
        expect(
138
          component.$el.querySelector('.card-assignee a').getAttribute('href'),
Phil Hughes's avatar
Phil Hughes committed
139
        ).toBe('/test');
Phil Hughes's avatar
Phil Hughes committed
140 141 142 143 144 145 146 147
      });

      it('renders avatar', () => {
        expect(
          component.$el.querySelector('.card-assignee img'),
        ).not.toBeNull();
      });
    });
148 149 150

    describe('assignee default avatar', () => {
      beforeEach((done) => {
151
        component.issue.assignees = [new ListAssignee({
152 153 154
          id: 1,
          name: 'testing 123',
          username: 'test',
155
        }, 'default_avatar')];
156 157 158 159 160 161 162 163 164 165 166 167 168

        Vue.nextTick(done);
      });

      it('displays defaults avatar if users avatar is null', () => {
        expect(
          component.$el.querySelector('.card-assignee img'),
        ).not.toBeNull();
        expect(
          component.$el.querySelector('.card-assignee img').getAttribute('src'),
        ).toBe('default_avatar');
      });
    });
Phil Hughes's avatar
Phil Hughes committed
169 170
  });

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  describe('multiple assignees', () => {
    beforeEach((done) => {
      component.issue.assignees = [
        user,
        new ListAssignee({
          id: 2,
          name: 'user2',
          username: 'user2',
          avatar: 'test_image',
        }),
        new ListAssignee({
          id: 3,
          name: 'user3',
          username: 'user3',
          avatar: 'test_image',
        }),
        new ListAssignee({
          id: 4,
          name: 'user4',
          username: 'user4',
          avatar: 'test_image',
        })];

      Vue.nextTick(() => done());
    });

    it('renders all four assignees', () => {
      expect(component.$el.querySelectorAll('.card-assignee .avatar').length).toEqual(4);
    });

    describe('more than four assignees', () => {
      beforeEach((done) => {
        component.issue.assignees.push(new ListAssignee({
          id: 5,
          name: 'user5',
          username: 'user5',
          avatar: 'test_image',
        }));

        Vue.nextTick(() => done());
      });

      it('renders more avatar counter', () => {
        expect(component.$el.querySelector('.card-assignee .avatar-counter').innerText).toEqual('+2');
      });

      it('renders three assignees', () => {
        expect(component.$el.querySelectorAll('.card-assignee .avatar').length).toEqual(3);
      });

      it('renders 99+ avatar counter', (done) => {
        for (let i = 5; i < 104; i += 1) {
          const u = new ListAssignee({
            id: i,
            name: 'name',
            username: 'username',
            avatar: 'test_image',
          });
          component.issue.assignees.push(u);
        }

        Vue.nextTick(() => {
          expect(component.$el.querySelector('.card-assignee .avatar-counter').innerText).toEqual('99+');
          done();
        });
      });
    });
  });

Phil Hughes's avatar
Phil Hughes committed
240
  describe('labels', () => {
Simon Knox's avatar
Simon Knox committed
241 242
    beforeEach((done) => {
      component.issue.addLabel(label1);
Phil Hughes's avatar
Phil Hughes committed
243

Simon Knox's avatar
Simon Knox committed
244 245
      Vue.nextTick(() => done());
    });
Phil Hughes's avatar
Phil Hughes committed
246

Simon Knox's avatar
Simon Knox committed
247 248 249 250 251 252 253 254 255 256
    it('renders list label', () => {
      expect(
        component.$el.querySelectorAll('.label').length,
      ).toBe(2);
    });

    it('renders label', () => {
      const nodes = [];
      component.$el.querySelectorAll('.label').forEach((label) => {
        nodes.push(label.title);
Phil Hughes's avatar
Phil Hughes committed
257 258
      });

Simon Knox's avatar
Simon Knox committed
259 260 261 262
      expect(
        nodes.includes(label1.description),
      ).toBe(true);
    });
Regis Boudinot's avatar
Regis Boudinot committed
263

Simon Knox's avatar
Simon Knox committed
264 265 266 267 268
    it('sets label description as title', () => {
      expect(
        component.$el.querySelector('.label').getAttribute('title'),
      ).toContain(label1.description);
    });
Phil Hughes's avatar
Phil Hughes committed
269

Simon Knox's avatar
Simon Knox committed
270 271 272 273
    it('sets background color of button', () => {
      const nodes = [];
      component.$el.querySelectorAll('.label').forEach((label) => {
        nodes.push(label.style.backgroundColor);
Phil Hughes's avatar
Phil Hughes committed
274 275
      });

Simon Knox's avatar
Simon Knox committed
276 277 278 279
      expect(
        nodes.includes(label1.color),
      ).toBe(true);
    });
Regis Boudinot's avatar
Regis Boudinot committed
280

Simon Knox's avatar
Simon Knox committed
281 282 283 284
    it('does not render label if label does not have an ID', (done) => {
      component.issue.addLabel(new ListLabel({
        title: 'closed',
      }));
285

Simon Knox's avatar
Simon Knox committed
286 287 288 289 290 291 292 293
      Vue.nextTick()
        .then(() => {
          expect(
            component.$el.querySelectorAll('.label').length,
          ).toBe(2);
          expect(
            component.$el.textContent,
          ).not.toContain('closed');
294

Simon Knox's avatar
Simon Knox committed
295 296 297
          done();
        })
        .catch(done.fail);
Phil Hughes's avatar
Phil Hughes committed
298 299 300
    });
  });
});