15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
import getArguments from "../getArguments.js";
import { knockout } from "../../Source/Cesium.js";
import { createCommand } from "../../Source/Cesium.js";
 
describe("Widgets/createCommand", function () {
  var spyFunction;
  var spyFunctionReturnValue = 5;
 
  beforeEach(function () {
    spyFunction = jasmine
      .createSpy("spyFunction")
      .and.returnValue(spyFunctionReturnValue);
  });
 
  it("works with default value of canExecute", function () {
    var command = createCommand(spyFunction);
    expect(command.canExecute).toBe(true);
    expect(command()).toBe(spyFunctionReturnValue);
    expect(spyFunction).toHaveBeenCalled();
  });
 
  it("throws when canExecute value is false", function () {
    var command = createCommand(spyFunction, false);
    expect(function () {
      command();
    }).toThrowDeveloperError();
    expect(spyFunction).not.toHaveBeenCalled();
  });
 
  it("throws without a func parameter", function () {
    expect(function () {
      return createCommand(undefined);
    }).toThrowDeveloperError();
  });
 
  it("works with custom canExecute observable", function () {
    var canExecute = knockout.observable(false);
    var command = createCommand(spyFunction, canExecute);
 
    expect(command.canExecute).toBe(false);
    expect(function () {
      command();
    }).toThrowDeveloperError();
    expect(spyFunction).not.toHaveBeenCalled();
 
    canExecute(true);
 
    expect(command.canExecute).toBe(true);
    expect(command()).toBe(spyFunctionReturnValue);
    expect(spyFunction).toHaveBeenCalled();
  });
 
  it("calls pre/post events", function () {
    var command = createCommand(spyFunction);
    var myArg = {};
 
    var beforeExecuteSpy = jasmine.createSpy("beforeExecute");
    command.beforeExecute.addEventListener(beforeExecuteSpy);
 
    var afterExecuteSpy = jasmine.createSpy("afterExecute");
    command.afterExecute.addEventListener(afterExecuteSpy);
 
    expect(command(myArg)).toBe(spyFunctionReturnValue);
 
    expect(beforeExecuteSpy.calls.count()).toEqual(1);
    expect(beforeExecuteSpy).toHaveBeenCalledWith({
      cancel: false,
      args: getArguments(myArg),
    });
 
    expect(afterExecuteSpy.calls.count()).toEqual(1);
    expect(afterExecuteSpy).toHaveBeenCalledWith(spyFunctionReturnValue);
  });
 
  it("cancels a command if beforeExecute sets cancel to true", function () {
    var command = createCommand(spyFunction);
    var myArg = {};
 
    var beforeExecuteSpy = jasmine
      .createSpy("beforeExecute")
      .and.callFake(function (info) {
        info.cancel = true;
      });
    command.beforeExecute.addEventListener(beforeExecuteSpy);
 
    var afterExecuteSpy = jasmine.createSpy("afterExecute");
    command.afterExecute.addEventListener(afterExecuteSpy);
 
    expect(command(myArg)).toBeUndefined();
 
    expect(beforeExecuteSpy.calls.count()).toEqual(1);
    expect(afterExecuteSpy).not.toHaveBeenCalled();
  });
});