updatereplicas_controller_test.js 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import UpdateReplicasDialogController from 'replicasetdetail/updatereplicas_controller';
import replicaSetDetailModule from 'replicasetdetail/replicasetdetail_module';

18
describe('Update Replicas controller', () => {
19 20
  /**
   * Replica Set Detail controller.
21
   * @type {!UpdateReplicasDialogController}
22 23 24 25
   */
  let ctrl;
  /** @type {!md.$dialog} */
  let mdDialog;
26 27
  /** @type {!ui.router.$state} */
  let state;
28 29 30 31 32 33
  /** @type {!angular.$resource} */
  let resource;
  /** @type {!angular.$httpBackend} */
  let httpBackend;
  /** @type {!angular.$log} */
  let log;
34

35 36 37 38
  /** @type {string} */
  let namespaceMock = 'foo-namespace';
  /** @type {string} */
  let replicaSetMock = 'foo-name';
39 40 41 42

  beforeEach(() => {
    angular.mock.module(replicaSetDetailModule.name);

43
    angular.mock.inject(($log, $state, $mdDialog, $controller, $httpBackend, $resource) => {
44
      mdDialog = $mdDialog;
45
      state = $state;
46 47 48
      resource = $resource;
      httpBackend = $httpBackend;
      log = $log;
49

50 51 52 53 54 55 56
      ctrl = $controller(UpdateReplicasDialogController, {
        $resource: resource,
        namespace: namespaceMock,
        replicaSet: replicaSetMock,
        currentPods: 1,
        desiredPods: 1,
      });
57 58 59
    });
  });

60
  it('should update controller replicas to given number and log success', () => {
61
    // given
62 63 64
    let replicaSpec = {
      replicas: 5,
    };
65
    spyOn(log, 'info');
66
    spyOn(state, 'reload');
67 68
    httpBackend.whenPOST('api/replicasets/foo-namespace/foo-name/update/pods')
        .respond(200, replicaSpec);
69 70

    // when
71 72
    ctrl.updateReplicas();
    httpBackend.flush();
73 74

    // then
75 76
    expect(log.info)
        .toHaveBeenCalledWith(`Successfully updated replicas number to ${replicaSpec.replicas}`);
77
    expect(state.reload).toHaveBeenCalled();
78 79
  });

80
  it('should log error on failed update', () => {
81 82
    // given
    spyOn(log, 'error');
83
    httpBackend.whenPOST('api/replicasets/foo-namespace/foo-name/update/pods').respond(404);
84 85

    // when
86 87
    ctrl.updateReplicas();
    httpBackend.flush();
88 89 90 91

    // then
    expect(log.error).toHaveBeenCalled();
  });
S
Sebastian Florek 已提交
92 93

  it('should close the dialog on cancel', () => {
94 95
    spyOn(state, 'reload');

S
Sebastian Florek 已提交
96 97 98 99 100 101 102 103
    // given
    spyOn(mdDialog, 'cancel');

    // when
    ctrl.cancel();

    // then
    expect(mdDialog.cancel).toHaveBeenCalled();
104
    expect(state.reload).not.toHaveBeenCalled();
S
Sebastian Florek 已提交
105
  });
106
});