-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(compute-key): add support for mapKeyToCacheKey #71
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b05c7fc
feat(compute-key): add support for mapKeyToCacheKey
code-forger a00bfa1
docs(readme): typo's and wording
code-forger e730c45
docs(readme): typo
code-forger 20b1a05
chore(tests): typo
code-forger ab34fdb
test(compute-key): test compute key more rigorously
code-forger 8babca8
Merge branch 'feat/mapKeyToCacheKey' of https://github.com/americanex…
code-forger a173369
chore(cleanup): remove defunct comment
code-forger edc3588
test(computekey): validate that options are passed to mapKeyToCacheKey
code-forger 0f72c1a
test(computekey): simplify test case
code-forger a693a07
test(computekey): more rigorous testing of compute key
code-forger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -14,9 +14,18 @@ | |
* permissions and limitations under the License. | ||
*/ | ||
|
||
import computeHash from 'object-hash'; | ||
import { computeKey } from '../src/computeKey'; | ||
|
||
jest.mock('object-hash', () => { | ||
const originalComputeHash = jest.requireActual('object-hash'); | ||
return jest.fn(originalComputeHash); | ||
}); | ||
|
||
describe('computeKey', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('should return an object', () => { | ||
expect(computeKey('abcd', {})).toMatchInlineSnapshot(` | ||
Object { | ||
|
@@ -58,4 +67,55 @@ describe('computeKey', () => { | |
}; | ||
expect(computeKey('uri', firstOptions).hash).toBe(computeKey('uri', secondOptions).hash); | ||
}); | ||
|
||
it('should return a different, stable hash, if the option mapKeyToCacheKey is passed', () => { | ||
expect(computeKey(() => 'abcd', { | ||
mapKeyToCacheKey: () => 'efgh', | ||
})).toMatchInlineSnapshot(` | ||
Object { | ||
"hash": "a0e09d568bb5b47c046b0fac7a61ca10196151cc", | ||
"key": "abcd", | ||
code-forger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
`); | ||
}); | ||
|
||
it('should pass generated cacheKey to the underlying hash function along with the options, and return the un-mapped key to the caller', () => { | ||
const computedKey = computeKey(() => 'abcd', { | ||
mapKeyToCacheKey: (key, options => `${key.toUpperCase()}-${options.optionKeyMock}`, | ||
optionKeyMock: 'optionKeyValue', | ||
}); | ||
expect(computedKey.key).toBe('abcd'); | ||
expect(computeHash).toHaveBeenCalledWith(['ABCD-optionKeyValue', { optionKeyMock: 'optionKeyValue' }], { respectType: false }); | ||
}); | ||
|
||
it('should return the same key if the option mapKeyToCacheKey returns the same string as the key', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has the same issue as the one on line 71 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed in a693a07 |
||
expect(computeKey(() => 'abcd', { | ||
mapKeyToCacheKey: key => key, | ||
})).toMatchInlineSnapshot(` | ||
Object { | ||
"hash": "037ace2918f4083eda9c4be34cccb93de5051b5a", | ||
"key": "abcd", | ||
} | ||
`); | ||
}); | ||
|
||
it('should return false if mapKeyToCacheKey throws error', () => { | ||
expect( | ||
computeKey(() => 'abcd', { | ||
mapKeyToCacheKey: () => { | ||
throw new Error('error'); | ||
}, | ||
}) | ||
).toEqual(false); | ||
}); | ||
|
||
it('should return false if mapKeyToCacheKey returns false', () => { | ||
expect(computeKey(() => 'abcd', { mapKeyToCacheKey: () => false })).toEqual(false); | ||
}); | ||
|
||
it('should1 throw an error if mapKeyToCacheKey is defined and not a function', () => { | ||
code-forger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(() => computeKey(() => 'abcd', | ||
{ mapKeyToCacheKey: 'string' } | ||
)).toThrow('mapKeyToCacheKey must be a function'); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test cannot assert that the key is different as the spec states since it only generates one key. There is nothing to compare it to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in a693a07