Skip to content

make isPromise() return boolean value for null and undefined cases #6785

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

Merged
merged 8 commits into from
Apr 7, 2021

Conversation

Jayasankar-m
Copy link
Contributor

@Jayasankar-m Jayasankar-m commented Dec 24, 2018

  • By placing an X in the preceding checkbox, I verify that I have signed the Contributor License Agreement

  • Currently isPromise() returns non boolean for the cases like following
    isPromise(null) // returns null
    isPromise(undefined) // returns undefined

  • Modified to return boolean value explicitly for the cases mentioned above


This change is Reviewable

@shs96c shs96c added the C-nodejs JavaScript Bindings label Dec 28, 2018
Copy link
Contributor

@martin770 martin770 left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @Jayasankar-m)


javascript/node/selenium-webdriver/lib/promise.js, line 38 at r1 (raw file):

    // Use array notation so the Closure compiler does not obfuscate away our
    // contract.
    return value != null

!!value to coerce the value to a boolean (instead of value != null)

Also, you should update the tests since they currently do nothing...

/test/lib/promise_test.js -- remove lines 59&60, replace with this test:

it('isPromise', () => {
const v = () => {};
const x = new Promise(v, v);
const p = createRejectedPromise('reject');
assert.equal(true, promise.isPromise(x));
assert.equal(true, promise.isPromise(p));
assert.equal(false, promise.isPromise(0));
assert.equal(false, promise.isPromise(false));
assert.equal(false, promise.isPromise(true));
assert.equal(false, promise.isPromise(null));
assert.equal(false, promise.isPromise(undefined));
assert.equal(false, promise.isPromise(''));
assert.equal(false, promise.isPromise('promise'));
assert.equal(false, promise.isPromise(v));
});

Copy link
Contributor

@martin770 martin770 left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r1.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Jayasankar-m)


javascript/node/selenium-webdriver/lib/promise.js, line 38 at r1 (raw file):

Previously, martin770 (James Martin) wrote…

!!value to coerce the value to a boolean (instead of value != null)

Also, you should update the tests since they currently do nothing...

/test/lib/promise_test.js -- remove lines 59&60, replace with this test:

it('isPromise', () => {
const v = () => {};
const x = new Promise(v, v);
const p = createRejectedPromise('reject');
assert.equal(true, promise.isPromise(x));
assert.equal(true, promise.isPromise(p));
assert.equal(false, promise.isPromise(0));
assert.equal(false, promise.isPromise(false));
assert.equal(false, promise.isPromise(true));
assert.equal(false, promise.isPromise(null));
assert.equal(false, promise.isPromise(undefined));
assert.equal(false, promise.isPromise(''));
assert.equal(false, promise.isPromise('promise'));
assert.equal(false, promise.isPromise(v));
});

In fact, you can completely remove the value && part and just have:

        return (typeof value === 'object' || typeof value === 'function')
        && typeof value['then'] === 'function';

And it will still pass all of those tests.

Copy link
Contributor Author

@Jayasankar-m Jayasankar-m left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @martin770)


javascript/node/selenium-webdriver/lib/promise.js, line 38 at r1 (raw file):

Previously, martin770 (James Martin) wrote…

In fact, you can completely remove the value && part and just have:

        return (typeof value === 'object' || typeof value === 'function')
        && typeof value['then'] === 'function';

And it will still pass all of those tests.

Done.
Updated as per review comments

Copy link
Contributor

@martin770 martin770 left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r2.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@CLAassistant
Copy link

CLAassistant commented Nov 23, 2019

CLA assistant check
All committers have signed the CLA.

@diemol diemol closed this Jul 12, 2020
@diemol diemol reopened this Jul 12, 2020
@diemol diemol changed the base branch from master to trunk July 12, 2020 20:15
@diemol
Copy link
Member

diemol commented Apr 7, 2021

@Jayasankar-m, would you like to continue with this PR?

@diemol diemol added the J-awaiting answer Question asked of user; a reply moves it to triage again label Apr 7, 2021
@Jayasankar-m
Copy link
Contributor Author

@Jayasankar-m, would you like to continue with this PR?

@diemol I guess all the required changes are already there in PR. Just needs to merge.
Let me know if anything is missing

@diemol
Copy link
Member

diemol commented Apr 7, 2021

@Jayasankar-m, there are merge conflicts, could you please have a look?

@sonarqubecloud
Copy link

sonarqubecloud bot commented Apr 7, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@Jayasankar-m
Copy link
Contributor Author

@Jayasankar-m, there are merge conflicts, could you please have a look?

@diemol it seems fix to the issue was made by another commit.
The tests written for this are included in the PR

Copy link
Member

@diemol diemol left a comment

Choose a reason for hiding this comment

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

You are right, a recent PR merge fixed this. I will merge the test, thank you.

@diemol diemol merged commit e9ba4e3 into SeleniumHQ:trunk Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-nodejs JavaScript Bindings J-awaiting answer Question asked of user; a reply moves it to triage again
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants