aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/result/transform_test.html
blob: 97a20ba7e7ac6161e73e874cbc0a0de8c5d1c361 (plain)
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
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
125
126
127
128
129
130
131
<!DOCTYPE html>
<html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<title>Closure Unit Tests - goog.result.transform</title>
<script src="../base.js"></script>
<script>

goog.require('goog.Timer');
goog.require('goog.result.SimpleResult');
goog.require('goog.result');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');

</script>
</head>
<body>
<script>

var result, resultCallback, multiplyResult, mockClock;

function setUpPage() {
  mockClock = new goog.testing.MockClock();
  mockClock.install();
}

function setUp() {
  mockClock.reset();
  result = new goog.result.SimpleResult();
  resultCallback = new goog.testing.recordFunction();
  multiplyResult = goog.testing.recordFunction(function(value) {
      return value * 2;
    });
}

function tearDown() {
  result = multiplyResult = null;
}

function tearDownPage() {
  mockClock.uninstall();
  goog.dispose(mockClock);
}

function testTransformWhenResultSuccess() {
  var transformedResult = goog.result.transform(result, multiplyResult);
  goog.result.wait(transformedResult, resultCallback);

  assertEquals(goog.result.Result.State.PENDING, result.getState());
  result.setValue(1);
  assertTransformerCall(multiplyResult, 1);
  assertSuccessCall(resultCallback, transformedResult, 2);
}

function testTransformWhenResultSuccessAsync() {
  var transformedResult = goog.result.transform(result, multiplyResult);
  goog.result.wait(transformedResult, resultCallback);

  goog.Timer.callOnce(function() {
    result.setValue(1);
  });

  assertEquals(goog.result.Result.State.PENDING, result.getState());
  mockClock.tick();
  assertTransformerCall(multiplyResult, 1);
  assertSuccessCall(resultCallback, transformedResult, 2);
}

function testTransformWhenResultError() {
  var transformedResult = goog.result.transform(result, multiplyResult);
  goog.result.wait(transformedResult, resultCallback);

  assertEquals(goog.result.Result.State.PENDING, result.getState());
  result.setError(4);
  assertNoCall(multiplyResult);
  assertErrorCall(resultCallback, transformedResult, 4);
}

function testTransformWhenResultErrorAsync() {
  var transformedResult = goog.result.transform(result, multiplyResult);

  goog.result.wait(transformedResult, resultCallback);

  goog.Timer.callOnce(function() {
    result.setError(5);
  });

  assertEquals(goog.result.Result.State.PENDING, result.getState());
  mockClock.tick();
  assertNoCall(multiplyResult);
  assertErrorCall(resultCallback, transformedResult, 5);
}

function assertSuccessCall(recordFunction, result, value) {
  assertEquals(1, recordFunction.getCallCount());

  var res = recordFunction.popLastCall().getArgument(0);
  assertEquals(result, res);
  assertEquals(goog.result.Result.State.SUCCESS, res.getState());
  assertEquals(value, res.getValue());
}

function assertErrorCall(recordFunction, result, value) {
  assertEquals(1, recordFunction.getCallCount());

  var res = recordFunction.popLastCall().getArgument(0);
  assertEquals(result, res);
  assertEquals(goog.result.Result.State.ERROR, res.getState());
  assertEquals(value, res.getError());
}

function assertNoCall(recordFunction) {
  assertEquals(0, recordFunction.getCallCount());
}

function assertTransformerCall(recordFunction, value) {
  assertEquals(1, recordFunction.getCallCount());

  var argValue = recordFunction.popLastCall().getArgument(0);
  assertEquals(value, argValue);
}

</script>
</body>
</html>