replicasetcardmenu_controller_test.js 3.4 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
// 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 {StateParams} from 'replicasetdetail/replicasetdetail_state';
import ReplicaSetCardMenuController from 'replicasetlist/replicasetcardmenu_controller';
import replicaSetListModule from 'replicasetlist/replicasetlist_module';

describe('Replica set card menu controller', () => {
  /** @type {!ReplicaSetCardMenuController} */
  let ctrl;
  /** @type {!ui.router.$state} */
  let state;
  /** @type {!angular.Scope} */
  let scope;
26 27
  /** @type {!replicasetdetail/replicaset_service.ReplicaSetService} */
  let kdReplicaSetService;
28 29 30 31

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

32
    angular.mock.inject(($controller, $state, _kdReplicaSetService_, $rootScope) => {
33
      state = $state;
34
      kdReplicaSetService = _kdReplicaSetService_;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
      scope = $rootScope;
      ctrl = $controller(
          ReplicaSetCardMenuController, null,
          {replicaSet: {name: 'foo-name', namespace: 'foo-namespace'}});
    });
  });

  it('should view details', () => {
    // given
    spyOn(state, 'go');

    // when
    ctrl.viewDetails();

    // then
B
bryk 已提交
50 51
    expect(state.go).toHaveBeenCalledWith(
        'replicasetdetail', new StateParams('foo-namespace', 'foo-name'));
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  });

  it('should open the menu', () => {
    // given
    let openMenuFn = jasmine.createSpy();
    let event = {};

    // when
    ctrl.openMenu(openMenuFn, event);

    // then
    expect(openMenuFn).toHaveBeenCalledWith(event);
  });

  it('should reload on successful delete', angular.mock.inject(($q) => {
    // given
    let deferred = $q.defer();
    spyOn(state, 'reload');
70
    spyOn(kdReplicaSetService, 'showDeleteDialog').and.returnValue(deferred.promise);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    // when
    ctrl.showDeleteDialog();
    deferred.resolve();

    // then
    expect(state.reload).not.toHaveBeenCalled();
    scope.$apply();
    expect(state.reload).toHaveBeenCalled();
  }));

  it('should log on delete error', angular.mock.inject(($q, $log) => {
    // given
    let deferred = $q.defer();
    spyOn($log, 'error');
    spyOn(state, 'reload');
87
    spyOn(kdReplicaSetService, 'showDeleteDialog').and.returnValue(deferred.promise);
88 89 90 91 92 93 94 95 96 97 98 99

    // when
    ctrl.showDeleteDialog();
    deferred.reject();

    // then
    expect(state.reload).not.toHaveBeenCalled();
    expect($log.error).not.toHaveBeenCalled();
    scope.$apply();
    expect(state.reload).not.toHaveBeenCalled();
    expect($log.error).toHaveBeenCalled();
  }));
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

  it('should show update replicas dialog', () => {
    // given
    ctrl.replicaSet = {
      namespace: '',
      name: '',
      pods: {
        current: 1,
        desired: 1,
      },
    };
    spyOn(kdReplicaSetService, 'showUpdateReplicasDialog');

    // when
    ctrl.showUpdateReplicasDialog();

    // then
    expect(kdReplicaSetService.showUpdateReplicasDialog).toHaveBeenCalled();
  });
119
});