Skip to content
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

DTPPSDK-275: Fix hashStr function and unit test coverage #141

Merged
merged 2 commits into from
Aug 17, 2021

Conversation

borodovisin
Copy link
Contributor

Fixed overflow problem with the hashStr function and unit test coverage

@borodovisin borodovisin requested a review from a team as a code owner August 17, 2021 17:00
@borodovisin borodovisin changed the title test(utils): fix hashStr function and unit test DTPPSDK-275 test(utils): fix hashStr function and unit test Aug 17, 2021
@borodovisin borodovisin changed the title DTPPSDK-275 test(utils): fix hashStr function and unit test DTPPSDK-275: Fix hashStr function and unit test coverage Aug 17, 2021
@codecov
Copy link

codecov bot commented Aug 17, 2021

Codecov Report

Merging #141 (4210487) into main (002059d) will increase coverage by 0.10%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #141      +/-   ##
==========================================
+ Coverage   94.40%   94.51%   +0.10%     
==========================================
  Files           7        7              
  Lines         161      164       +3     
  Branches       55       56       +1     
==========================================
+ Hits          152      155       +3     
  Misses          9        9              
Impacted Files Coverage Δ
src/utils.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 002059d...4210487. Read the comment docs.


describe("hashStr", () => {
test("should match the hash from the argument string", () => {
expect(hashStr("react")).toMatchSnapshot();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be a bit picky on this comment, but generally speaking for tests like this I'd almost rather just see the expected string inline vs in a snapshot. I find when everything is contained in one file its easier to quickly see expected behavior. Given that these are shorter strings I think inlining would be easier than snapshots, but I am totally open to whatever folks feel is best!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I can use the toMatchInlineSnapshot because I'm calling a hash function and don't know from the top of my head the result. Let me know if it is better in that way...


describe("hashStr", () => {
test("should match the hash from the argument string", () => {
expect(hashStr("react")).toMatchInlineSnapshot(`"xxhjw"`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about Inline Snapshots. Very cool!

https://jestjs.io/docs/snapshot-testing#inline-snapshots

Copy link
Contributor

@gregjopa gregjopa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @borodovisin!

Copy link
Member

@westeezy westeezy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

for (let i = 0; i < str.length; i++) {
hash += str[i].charCodeAt(0) * Math.pow((i % 10) + 1, 5);
let total = str[i].charCodeAt(0) * i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does change the characteristics of the generated hash. I just think we should double check that it we are okay with that and that we don't forsee any issues.

*/
export function hashStr(str: string): number {
let hash = 0;
export function hashStr(str: string): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there were some previous discussions around the hashStr function in belter which this is sourced from. Maybe we can make an issue in belter to acknowledge there might be overflow problems with that method of generating a hash


describe("hashStr", () => {
test("should match the hash from the argument string", () => {
expect(hashStr("react")).toMatchInlineSnapshot(`"xxhjw"`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old thread got marked outdated, but i dont think you need the snapshot methods at all since the hashing is deterministic right? so you could just have expect(hashStr("react")).toEqual("xxhjw"); correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is right, the only think I do not create the string, that was generate from the jest library, because that I not use the toEqual. To make that I need to run the function externally and copy the result.

@gregjopa gregjopa merged commit 49b949d into paypal:main Aug 17, 2021
gregjopa pushed a commit that referenced this pull request Aug 17, 2021
The 'data-react-paypal-script-id' attribute uses the hash value from
this `hashStr(JSON.stringify(options))` function. This change fixes
an issue where a large `options` object could cause a numeric overflow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants