forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
superset-frontend/spec/javascripts/utils/cacheWrapper_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 { cacheWrapper } from 'src/utils/cacheWrapper'; | ||
|
||
describe('cacheWrapper', () => { | ||
const fnResult = 'fnResult'; | ||
const fn = jest.fn<string, [number, number]>().mockReturnValue(fnResult); | ||
|
||
let wrappedFn: (a: number, b: number) => string; | ||
|
||
beforeEach(() => { | ||
const cache = new Map<string, any>(); | ||
wrappedFn = cacheWrapper(fn, cache); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('calls fn with its arguments once when the key is not found', () => { | ||
const returnedValue = wrappedFn(1, 2); | ||
|
||
expect(returnedValue).toEqual(fnResult); | ||
expect(fn).toBeCalledTimes(1); | ||
expect(fn).toBeCalledWith(1, 2); | ||
}); | ||
|
||
describe('subsequent calls', () => { | ||
it('returns the correct value without fn being called multiple times', () => { | ||
const returnedValue1 = wrappedFn(1, 2); | ||
const returnedValue2 = wrappedFn(1, 2); | ||
|
||
expect(returnedValue1).toEqual(fnResult); | ||
expect(returnedValue2).toEqual(fnResult); | ||
expect(fn).toBeCalledTimes(1); | ||
}); | ||
|
||
it('fn is called multiple times for different arguments', () => { | ||
wrappedFn(1, 2); | ||
wrappedFn(1, 3); | ||
|
||
expect(fn).toBeCalledTimes(2); | ||
}); | ||
}); | ||
|
||
describe('with custom keyFn', () => { | ||
let cache: Map<string, any>; | ||
|
||
beforeEach(() => { | ||
cache = new Map<string, any>(); | ||
wrappedFn = cacheWrapper(fn, cache, (...args) => `key-${args[0]}`); | ||
}); | ||
|
||
it('saves fn result in cache under generated key', () => { | ||
wrappedFn(1, 2); | ||
expect(cache.get('key-1')).toEqual(fnResult); | ||
}); | ||
|
||
it('subsequent calls with same generated key calls fn once, even if other arguments have changed', () => { | ||
wrappedFn(1, 1); | ||
wrappedFn(1, 2); | ||
wrappedFn(1, 3); | ||
|
||
expect(fn).toBeCalledTimes(1); | ||
}); | ||
}); | ||
}); |