| 1 |
// Copyright 2017 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.module('goog.delegate.delegatesTest'); |
| 16 |
goog.setTestOnly(); |
| 17 |
|
| 18 |
const delegates = goog.require('goog.delegate.delegates'); |
| 19 |
const recordFunction = goog.require('goog.testing.recordFunction'); |
| 20 |
const testSuite = goog.require('goog.testing.testSuite'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
testSuite({ |
| 24 |
|
| 25 |
shouldRunTests() { |
| 26 |
return typeof Array.prototype.map == 'function'; |
| 27 |
}, |
| 28 |
|
| 29 |
testFunctionsActuallyCalled() { |
| 30 |
const funcs = [ |
| 31 |
recordFunction(), |
| 32 |
recordFunction(() => ''), |
| 33 |
recordFunction(() => 42), |
| 34 |
recordFunction(), |
| 35 |
]; |
| 36 |
const assertCallCounts = (...counts) => { |
| 37 |
assertArrayEquals(counts, funcs.map(f => f.getCallCount())); |
| 38 |
funcs.forEach(f => f.reset()); |
| 39 |
}; |
| 40 |
|
| 41 |
assertUndefined(delegates.callFirst(funcs, f => f())); |
| 42 |
assertCallCounts(1, 0, 0, 0); |
| 43 |
assertEquals('', delegates.callUntilDefinedAndNotNull(funcs, f => f())); |
| 44 |
assertCallCounts(1, 1, 0, 0); |
| 45 |
assertEquals(42, delegates.callUntilTruthy(funcs, f => f())); |
| 46 |
assertCallCounts(1, 1, 1, 0); |
| 47 |
}, |
| 48 |
|
| 49 |
testResultNeverDefined() { |
| 50 |
const funcs = [ |
| 51 |
recordFunction(), |
| 52 |
recordFunction(), |
| 53 |
]; |
| 54 |
const assertCallCounts = (...counts) => { |
| 55 |
assertArrayEquals(counts, funcs.map(f => f.getCallCount())); |
| 56 |
funcs.forEach(f => f.reset()); |
| 57 |
}; |
| 58 |
|
| 59 |
assertUndefined(delegates.callFirst(funcs, f => f())); |
| 60 |
assertCallCounts(1, 0); |
| 61 |
assertUndefined(delegates.callUntilDefinedAndNotNull(funcs, f => f())); |
| 62 |
assertCallCounts(1, 1); |
| 63 |
assertFalse(delegates.callUntilTruthy(funcs, f => f())); |
| 64 |
assertCallCounts(1, 1); |
| 65 |
}, |
| 66 |
}); |