deployfromsettings_controller_test.js 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 DeployFromSettingController from 'deploy/deployfromsettings_controller';
import deployModule from 'deploy/deploy_module';
17
import DeployLabel from 'deploy/deploylabel';
18 19

describe('DeployFromSettings controller', () => {
20
  /** @type {!DeployFromSettingController} */
21
  let ctrl;
22 23
  /** @type {!angular.$resource} */
  let mockResource;
24 25 26 27

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

28
    angular.mock.inject(($controller) => {
29 30
      mockResource = jasmine.createSpy('$resource');
      ctrl = $controller(DeployFromSettingController, {$resource: mockResource}, {namespaces: []});
31 32 33 34
    });
  });

  it('should return empty string when containerImage is undefined', () => {
35
    // given
36
    ctrl.containerImage = undefined;
37 38 39 40 41 42

    // when
    let result = ctrl.getContainerImageVersion_();

    // then
    expect(result).toEqual('');
43 44 45
  });

  it('should return empty string when containerImage is empty', () => {
46
    // given
47
    ctrl.containerImage = '';
48 49 50 51 52 53

    // when
    let result = ctrl.getContainerImageVersion_();

    // then
    expect(result).toEqual('');
54 55 56 57 58
  });

  it('should return empty string when containerImage is not empty and does not contain `:`' +
         ' delimiter',
     () => {
59
       // given
60
       ctrl.containerImage = 'test';
61 62 63 64 65 66

       // when
       let result = ctrl.getContainerImageVersion_();

       // then
       expect(result).toEqual('');
67 68 69
     });

  it('should return part of the string after `:` delimiter', () => {
70
    // given
71
    ctrl.containerImage = 'test:1';
72 73 74 75 76 77

    // when
    let result = ctrl.getContainerImageVersion_();

    // then
    expect(result).toEqual('1');
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
  it('should return empty array when labels array is empty', () => {
    // given
    let labels = [];

    // when
    let result = ctrl.toBackendApiLabels_(labels);

    // then
    expect(result).toEqual([]);
  });

  it('should return filtered backend api labels array without empty key/values', () => {
    // given
    let labels = [
      new DeployLabel('', 'val'),
      new DeployLabel('key1', 'val1'),
    ];

    // when
    let result = ctrl.toBackendApiLabels_(labels);

    // then
    let expected = [
      {
        key: 'key1',
        value: 'val1',
      },
    ];

    expect(result).toEqual(expected);
  });
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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

  it('should deploy empty description and container commands as nulls', () => {
    // given
    let resourceObject = {
      save: jasmine.createSpy('save'),
    };
    mockResource.and.returnValue(resourceObject);
    resourceObject.save.and.callFake(function(spec) {
      // then
      expect(spec.containerCommand).toBeNull();
      expect(spec.containerCommandArgs).toBeNull();
      expect(spec.description).toBeNull();
    });
    ctrl.labels = [
      new DeployLabel('key1', 'val1'),
    ];

    // when
    ctrl.deploy();

    // then
    expect(resourceObject.save).toHaveBeenCalled();
  });

  it('should deploy non empty description and container commands', () => {
    // given
    let resourceObject = {
      save: jasmine.createSpy('save'),
    };
    mockResource.and.returnValue(resourceObject);
    resourceObject.save.and.callFake(function(spec) {
      // then
      expect(spec.containerCommand).toBe('command');
      expect(spec.containerCommandArgs).toBe('commandArgs');
      expect(spec.description).toBe('desc');
    });
    ctrl.labels = [
      new DeployLabel('key1', 'val1'),
    ];
    ctrl.containerCommand = 'command';
    ctrl.containerCommandArgs = 'commandArgs';
    ctrl.description = 'desc';

    // when
    ctrl.deploy();

    // then
    expect(resourceObject.save).toHaveBeenCalled();
  });
160
});