|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | import assert from 'assert'
|
7 |
| -import { once, onceChanged, debounce, oncePerUniqueArg } from '../../../shared/utilities/functionUtils' |
| 7 | +import { |
| 8 | + once, |
| 9 | + onceChanged, |
| 10 | + debounce, |
| 11 | + oncePerUniqueArg, |
| 12 | + onceChangedWithComparator, |
| 13 | +} from '../../../shared/utilities/functionUtils' |
8 | 14 | import { installFakeClock } from '../../testUtil'
|
9 | 15 |
|
10 | 16 | describe('functionUtils', function () {
|
@@ -49,6 +55,36 @@ describe('functionUtils', function () {
|
49 | 55 | assert.strictEqual(counter, 3)
|
50 | 56 | })
|
51 | 57 |
|
| 58 | + it('onceChangedWithComparator()', function () { |
| 59 | + let counter = 0 |
| 60 | + const credentialsEqual = ([prev]: [any], [current]: [any]) => { |
| 61 | + if (!prev && !current) { |
| 62 | + return true |
| 63 | + } |
| 64 | + if (!prev || !current) { |
| 65 | + return false |
| 66 | + } |
| 67 | + return prev.accessKeyId === current.accessKeyId && prev.secretAccessKey === current.secretAccessKey |
| 68 | + } |
| 69 | + const fn = onceChangedWithComparator((creds: any) => void counter++, credentialsEqual) |
| 70 | + |
| 71 | + const creds1 = { accessKeyId: 'key1', secretAccessKey: 'secret1' } |
| 72 | + const creds2 = { accessKeyId: 'key1', secretAccessKey: 'secret1' } |
| 73 | + const creds3 = { accessKeyId: 'key2', secretAccessKey: 'secret2' } |
| 74 | + |
| 75 | + fn(creds1) |
| 76 | + assert.strictEqual(counter, 1) |
| 77 | + |
| 78 | + fn(creds2) // Same values, should not execute |
| 79 | + assert.strictEqual(counter, 1) |
| 80 | + |
| 81 | + fn(creds3) // Different values, should execute |
| 82 | + assert.strictEqual(counter, 2) |
| 83 | + |
| 84 | + fn(creds3) // Same as previous, should not execute |
| 85 | + assert.strictEqual(counter, 2) |
| 86 | + }) |
| 87 | + |
52 | 88 | it('oncePerUniqueArg()', function () {
|
53 | 89 | let counter = 0
|
54 | 90 | const fn = oncePerUniqueArg((s: string) => {
|
|
0 commit comments