deployfromsettings_controller_test.js 5.7 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';
B
bryk 已提交
18
import {uniqueNameValidationKey} from 'deploy/uniquename_directive';
19 20

describe('DeployFromSettings controller', () => {
21
  /** @type {!DeployFromSettingController} */
22
  let ctrl;
23 24
  /** @type {!angular.$resource} */
  let mockResource;
B
bryk 已提交
25 26
  /** @type {!angular.FormController} */
  let form;
27 28 29 30

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

31
    angular.mock.inject(($controller) => {
32
      mockResource = jasmine.createSpy('$resource');
B
bryk 已提交
33 34 35 36 37 38 39 40 41 42 43 44
      form = {
        $submitted: false,
        name: {
          $touched: false,
          $invalid: false,
          $error: {
            [uniqueNameValidationKey]: false,
          },
        },
      };
      ctrl = $controller(
          DeployFromSettingController, {$resource: mockResource}, {namespaces: [], form: form});
45 46 47 48
    });
  });

  it('should return empty string when containerImage is undefined', () => {
49
    // given
50
    ctrl.containerImage = undefined;
51 52 53 54 55 56

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

    // then
    expect(result).toEqual('');
57 58 59
  });

  it('should return empty string when containerImage is empty', () => {
60
    // given
61
    ctrl.containerImage = '';
62 63 64 65 66 67

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

    // then
    expect(result).toEqual('');
68 69 70 71 72
  });

  it('should return empty string when containerImage is not empty and does not contain `:`' +
         ' delimiter',
     () => {
73
       // given
74
       ctrl.containerImage = 'test';
75 76 77 78 79 80

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

       // then
       expect(result).toEqual('');
81 82 83
     });

  it('should return part of the string after `:` delimiter', () => {
84
    // given
85
    ctrl.containerImage = 'test:1';
86 87 88 89 90 91

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

    // then
    expect(result).toEqual('1');
92 93
  });

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  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);
  });
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173

  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();
  });
174 175 176 177

  it('should hide more options by default', () => {
    // this is default behavior so no given/when
    // then
B
bryk 已提交
178
    expect(ctrl.isMoreOptionsEnabled()).toBe(false);
179 180 181 182 183 184 185
  });

  it('should show more options after switch', () => {
    // when
    ctrl.switchMoreOptions();

    // then
B
bryk 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    expect(ctrl.isMoreOptionsEnabled()).toBe(true);
  });

  describe('isNameError', () => {
    it('should show all errors on submit', () => {
      expect(ctrl.isNameError()).toBe(false);

      form.name.$invalid = true;

      expect(ctrl.isNameError()).toBe(false);

      form.$submitted = true;

      expect(ctrl.isNameError()).toBe(true);
    });

    it('should show all errors when touched', () => {
      expect(ctrl.isNameError()).toBe(false);

      form.name.$invalid = true;

      expect(ctrl.isNameError()).toBe(false);

      form.name.$touched = true;

      expect(ctrl.isNameError()).toBe(true);
    });

    it('should always show name uniqueness errors', () => {
      expect(ctrl.isNameError()).toBe(false);

      form.name.$error[uniqueNameValidationKey] = true;

      expect(ctrl.isNameError()).toBe(true);

      form.$submitted = true;

      expect(ctrl.isNameError()).toBe(true);
    });
225
  });
226
});