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

feat: add possibility to handle access geolocation dialog(closes #3224) #7791

Merged
merged 6 commits into from
Jul 12, 2023

Conversation

Artem-Babich
Copy link
Contributor

@Artem-Babich Artem-Babich commented Jun 13, 2023

Purpose

#3224

In some cases, you need to handle access geolocation dialog and provide a corresponding result.

Approach

The geolocation window can be invoked when the user call window.navigator.geolocation.getCurrentPosition or watchPosition.
We cannot emulate watchPosition method because it invokes callbacks on changing user geolocation. So, this PR only handles getCurrentPosition method. When this method is called on the page for the first time, the Geolocation access dialog must be shown. All subsequent calls will lead to returning geolocation in the case when the user allowed access, or an error in the case when the user denied access.
This handler allows you to return a geolocation or an error when calling getCurrentPosition. If an instance of an Error object was returned, the failCallback function will be called with that error. If any other value is returned, then successCallback will be called with that value.
Since this dialog is only shown the first time getCurrentPosition is called, the history contains only one record per URL.

API

// Test
test('Expected geolocation object and geolocation error returned after an action', async t => {
  await t
    .setNativeDialogHandler((type) => {
        if (type === 'geolocation')
            return { timestamp: 12356, coords: {} };
    
        return null;
    });
    .click('#buttonGeo')
    .setNativeDialogHandler((type) => {
        if (type !== 'geolocation')
            return null;
    
        const err = new Error('Some error');
    
        err.code = 1;
    
        return err;
    })
    .click('#buttonGeo');
    
  const history = await t.getNativeDialogHistory();
  
  // NOTE: Only one record will be added to the history, since dialog appears only on the first geolocation request
  await t.expect(history).eql([{ type: 'geolocation', url: pageUrl }]);
});

// Aplication code:
function successCallback(geolocation) {
    console.log(geolocation) // { timestamp: 12356, coords: {} }
}
function failCallback(err) {
    console.log(err.message); //  "Some error"
    console.log(err.code); // 1
}

function onGeoButtonClick () {
    window.navigator.geolocation.getCurrentPosition(successCallback, failCallback)
}

References

#3224

Pre-Merge TODO

  • Write tests for your proposed changes
  • Make sure that existing tests do not fail

@Artem-Babich Artem-Babich temporarily deployed to CI June 13, 2023 15:16 — with GitHub Actions Inactive
@Artem-Babich Artem-Babich temporarily deployed to CI June 13, 2023 15:17 — with GitHub Actions Inactive
@Artem-Babich Artem-Babich temporarily deployed to CI June 13, 2023 16:22 — with GitHub Actions Inactive
@Artem-Babich Artem-Babich temporarily deployed to CI June 13, 2023 16:28 — with GitHub Actions Inactive
@Artem-Babich Artem-Babich temporarily deployed to CI July 12, 2023 08:20 — with GitHub Actions Inactive
@Artem-Babich Artem-Babich merged commit faed539 into DevExpress:master Jul 12, 2023
20 checks passed
@Artem-Babich Artem-Babich deleted the handle-geoposition branch July 12, 2023 11:29
@github-actions
Copy link

Release v3.1.0-rc.1 addresses this.

miherlosev pushed a commit that referenced this pull request Jul 20, 2023
## Purpose
If the user mocks the geolocation dialog via
page/client-scripts/client-function we will override it with default
native dialog handler. That will lead to unexpected behavior.
This PR also handles the case when the user "promisified" the
getCurrentPosition method which may lead to test hanging.

## Approach
Do not override the getCurrentPosition method in case when it is already
overridden.

## References
#7791

## Pre-Merge TODO
- [x] Write tests for your proposed changes
- [ ] Make sure that existing tests do not fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants