summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_shared/components/gl_countdown_spec.js
blob: 929ffe219f418e0db7fde23e559f205ef066e29d (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
import mountComponent from 'spec/helpers/vue_mount_component_helper';
import Vue from 'vue';
import GlCountdown from '~/vue_shared/components/gl_countdown.vue';

describe('GlCountdown', () => {
  const Component = Vue.extend(GlCountdown);
  let vm;
  let now = '2000-01-01T00:00:00Z';

  beforeEach(() => {
    spyOn(Date, 'now').and.callFake(() => new Date(now).getTime());
    jasmine.clock().install();
  });

  afterEach(() => {
    vm.$destroy();
    jasmine.clock().uninstall();
  });

  describe('when there is time remaining', () => {
    beforeEach(done => {
      vm = mountComponent(Component, {
        endDateString: '2000-01-01T01:02:03Z',
      });

      Vue.nextTick()
        .then(done)
        .catch(done.fail);
    });

    it('displays remaining time', () => {
      expect(vm.$el).toContainText('01:02:03');
    });

    it('updates remaining time', done => {
      now = '2000-01-01T00:00:01Z';
      jasmine.clock().tick(1000);

      Vue.nextTick()
        .then(() => {
          expect(vm.$el).toContainText('01:02:02');
          done();
        })
        .catch(done.fail);
    });
  });

  describe('when there is no time remaining', () => {
    beforeEach(done => {
      vm = mountComponent(Component, {
        endDateString: '1900-01-01T00:00:00Z',
      });

      Vue.nextTick()
        .then(done)
        .catch(done.fail);
    });

    it('displays 00:00:00', () => {
      expect(vm.$el).toContainText('00:00:00');
    });
  });

  describe('when an invalid date is passed', () => {
    it('throws a validation error', () => {
      spyOn(Vue.config, 'warnHandler').and.stub();
      vm = mountComponent(Component, {
        endDateString: 'this is invalid',
      });

      expect(Vue.config.warnHandler).toHaveBeenCalledTimes(1);
      const [errorMessage] = Vue.config.warnHandler.calls.argsFor(0);

      expect(errorMessage).toMatch(/^Invalid prop: .* "endDateString"/);
    });
  });
});