BigW Consortium Gitlab

poll_spec.js 5.58 KB
Newer Older
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
import Vue from 'vue';
import VueResource from 'vue-resource';
import Poll from '~/lib/utils/poll';

Vue.use(VueResource);

class ServiceMock {
  constructor(endpoint) {
    this.service = Vue.resource(endpoint);
  }

  fetch() {
    return this.service.get();
  }
}

describe('Poll', () => {
  let callbacks;

  beforeEach(() => {
    callbacks = {
      success: () => {},
      error: () => {},
    };

    spyOn(callbacks, 'success');
    spyOn(callbacks, 'error');
  });

  it('calls the success callback when no header for interval is provided', (done) => {
    const successInterceptor = (request, next) => {
      next(request.respondWith(JSON.stringify([]), { status: 200 }));
    };

    Vue.http.interceptors.push(successInterceptor);

    new Poll({
      resource: new ServiceMock('endpoint'),
      method: 'fetch',
      successCallback: callbacks.success,
      errorCallback: callbacks.error,
    }).makeRequest();

    setTimeout(() => {
      expect(callbacks.success).toHaveBeenCalled();
      expect(callbacks.error).not.toHaveBeenCalled();
      done();
    }, 0);

    Vue.http.interceptors = _.without(Vue.http.interceptors, successInterceptor);
  });

  it('calls the error callback whe the http request returns an error', (done) => {
    const errorInterceptor = (request, next) => {
      next(request.respondWith(JSON.stringify([]), { status: 500 }));
    };

    Vue.http.interceptors.push(errorInterceptor);

    new Poll({
      resource: new ServiceMock('endpoint'),
      method: 'fetch',
      successCallback: callbacks.success,
      errorCallback: callbacks.error,
    }).makeRequest();

    setTimeout(() => {
      expect(callbacks.success).not.toHaveBeenCalled();
      expect(callbacks.error).toHaveBeenCalled();
      done();
    }, 0);

    Vue.http.interceptors = _.without(Vue.http.interceptors, errorInterceptor);
  });

  it('should call the success callback when the interval header is -1', (done) => {
    const intervalInterceptor = (request, next) => {
      next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': -1 } }));
    };

    Vue.http.interceptors.push(intervalInterceptor);

    new Poll({
      resource: new ServiceMock('endpoint'),
      method: 'fetch',
      successCallback: callbacks.success,
      errorCallback: callbacks.error,
    }).makeRequest();

    setTimeout(() => {
      expect(callbacks.success).toHaveBeenCalled();
      expect(callbacks.error).not.toHaveBeenCalled();
      done();
    }, 0);

    Vue.http.interceptors = _.without(Vue.http.interceptors, intervalInterceptor);
  });

  it('starts polling when http status is 200 and interval header is provided', (done) => {
    const pollInterceptor = (request, next) => {
      next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
    };

    Vue.http.interceptors.push(pollInterceptor);

    const service = new ServiceMock('endpoint');
    spyOn(service, 'fetch').and.callThrough();

    new Poll({
      resource: service,
      method: 'fetch',
112
      data: { page: 1 },
113 114 115 116 117 118
      successCallback: callbacks.success,
      errorCallback: callbacks.error,
    }).makeRequest();

    setTimeout(() => {
      expect(service.fetch.calls.count()).toEqual(2);
119
      expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
120 121 122 123 124 125 126
      expect(callbacks.success).toHaveBeenCalled();
      expect(callbacks.error).not.toHaveBeenCalled();
      done();
    }, 5);

    Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
  });
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

  describe('stop', () => {
    it('stops polling when method is called', (done) => {
      const pollInterceptor = (request, next) => {
        next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
      };

      Vue.http.interceptors.push(pollInterceptor);

      const service = new ServiceMock('endpoint');
      spyOn(service, 'fetch').and.callThrough();

      const Polling = new Poll({
        resource: service,
        method: 'fetch',
        data: { page: 1 },
        successCallback: () => {
          Polling.stop();
        },
        errorCallback: callbacks.error,
      });

      spyOn(Polling, 'stop').and.callThrough();

      Polling.makeRequest();

      setTimeout(() => {
        expect(service.fetch.calls.count()).toEqual(1);
        expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
        expect(Polling.stop).toHaveBeenCalled();
        done();
      }, 100);

      Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
    });
  });
163 164 165 166 167 168 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

  describe('restart', () => {
    it('should restart polling when its called', (done) => {
      const pollInterceptor = (request, next) => {
        next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
      };

      Vue.http.interceptors.push(pollInterceptor);

      const service = new ServiceMock('endpoint');

      spyOn(service, 'fetch').and.callThrough();

      const Polling = new Poll({
        resource: service,
        method: 'fetch',
        data: { page: 1 },
        successCallback: () => {
          Polling.stop();
          setTimeout(() => {
            Polling.restart();
          }, 0);
        },
        errorCallback: callbacks.error,
      });

      spyOn(Polling, 'stop').and.callThrough();

      Polling.makeRequest();

      setTimeout(() => {
        expect(service.fetch.calls.count()).toEqual(2);
        expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
        expect(Polling.stop).toHaveBeenCalled();
        done();
      }, 10);

      Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
    });
  });
203
});