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

fix(navigator): check for experimental userAgentData first #1548

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ jobs:
- save_cache: *save_yarn_cache
- run: *restore_dist_folders
- run:
name: Cypress test Actions
command: yarn run cy:run
name: Cypress test on Chrome
command: yarn run cy:run:chrome
- run:
name: Cypress test on Firefox
command: yarn run cy:run:firefox

workflows:
version: 2
Expand Down
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module.exports = {
},
rules: {
'jest/expect-expect': 0,
'jest/valid-expect': 0,
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
'spaced-comment': 0,
'@typescript-eslint/triple-slash-reference': 0,
},
Expand Down
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"path": "packages/docsearch-js/dist/umd/index.js",
"maxSize": "28.20 kB"
"maxSize": "28.25 kB"
}
]
}
30 changes: 30 additions & 0 deletions cypress/integration/search/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,33 @@ describe('Recent and Favorites', () => {
cy.contains('No recent searches').should('be.visible');
});
});

describe('Navigator', () => {
it(
'Use correct navigator to detect Apple device on chrome',
{ browser: 'chrome' },
() => {
expect((navigator as any).userAgentData).not.be.undefined;
expect(navigator.platform).not.to.be.undefined;
Comment on lines +147 to +148
Copy link
Member

Choose a reason for hiding this comment

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

This is testing the Chrome platform, not DocSearch. Why would we test this?


cy.visit(Cypress.config().baseUrl!);

cy.get('body').type('{meta}k');
cy.modalIsVisibleAndFocused();
}
);

it(
'Use correct navigator to detect Apple device on non chrome browser',
{ browser: '!chrome' },
() => {
expect((navigator as any).userAgentData).to.be.undefined;
expect(navigator.platform).not.to.be.undefined;

cy.visit(Cypress.config().baseUrl!);

cy.get('body').type('{meta}k');
cy.modalIsVisibleAndFocused();
}
);
});
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
"build": "lerna run build --scope @docsearch/*",
"cy:clean": "rm -rf cypress/screenshots",
"cy:info": "cypress info",
"cy:run:chrome": "yarn run cy:run --browser chrome",
"cy:run:edge": "yarn run cy:run --browser edge",
"cy:run:firefox": "yarn run cy:run --browser firefox",
"cy:run": "start-server-and-test 'yarn website:test' http://localhost:3000 'cypress run --spec 'cypress/integration/**/*' --headless'",
"cy:run:chrome": "start-server-and-test 'yarn website:test' http://localhost:3000 'cypress run --browser chrome --spec 'cypress/integration/**/*' --headless'",
"cy:run:firefox": "start-server-and-test 'yarn website:test' http://localhost:3000 'cypress run --browser firefox --spec 'cypress/integration/**/*' --headless'",
"cy:verify": "cypress verify",
"lint:css": "stylelint **/src/**/*.css",
"lint": "eslint --ext .js,.ts,.tsx .",
Expand Down
6 changes: 5 additions & 1 deletion packages/docsearch-react/src/DocSearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const ACTION_KEY_DEFAULT = 'Ctrl' as const;
const ACTION_KEY_APPLE = '⌘' as const;

function isAppleDevice() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
return /(Mac|iPhone|iPod|iPad)/i.test(
// `userAgentData` is not supported everywhere and still experimental
// so types are not provided yet.
(navigator as any)?.userAgentData?.platform || navigator.platform
);
}

export const DocSearchButton = React.forwardRef<
Expand Down
23 changes: 13 additions & 10 deletions packages/docsearch-react/src/__tests__/api.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function noResultSearch(_queries: any, _requestOptions?: any): Promise<any> {
}

describe('api', () => {
let container: HTMLDivElement;
let container: HTMLDivElement | null;

const docSearchSelector = '.DocSearch';

Expand All @@ -44,8 +44,10 @@ describe('api', () => {
});

afterEach(() => {
document.body.removeChild(container);
container = null;
if (container) {
document.body.removeChild(container);
container = null;
}
});

it('renders with minimal parameters', () => {
Expand All @@ -68,10 +70,10 @@ describe('api', () => {
);
expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
expect(
document.querySelector('.DocSearch-Button-Placeholder').innerHTML
document.querySelector('.DocSearch-Button-Placeholder')!.innerHTML
).toBe('Recherche');
expect(
document.querySelector('.DocSearch-Button').getAttribute('aria-label')
document.querySelector('.DocSearch-Button')!.getAttribute('aria-label')
).toBe('Recherche');
});

Expand Down Expand Up @@ -166,17 +168,17 @@ describe('api', () => {
fireEvent.click(await screen.findByText('Search'));
});

expect(document.querySelector('.DocSearch-Cancel').innerHTML).toBe(
expect(document.querySelector('.DocSearch-Cancel')!.innerHTML).toBe(
'Annuler'
);
expect(
document.querySelector('.DocSearch-Cancel').getAttribute('aria-label')
document.querySelector('.DocSearch-Cancel')!.getAttribute('aria-label')
).toBe('Annuler');
expect(
document.querySelector('.DocSearch-Reset').getAttribute('title')
document.querySelector('.DocSearch-Reset')!.getAttribute('title')
).toBe('Effacer');
expect(
document.querySelector('.DocSearch-Reset').getAttribute('aria-label')
document.querySelector('.DocSearch-Reset')!.getAttribute('aria-label')
).toBe('Effacer');
});

Expand Down Expand Up @@ -290,9 +292,10 @@ describe('api', () => {
});

expect(screen.getByText(/No results for/)).toBeInTheDocument();

const link = document.querySelector('.DocSearch-Help a');
expect(link).toBeInTheDocument();
expect(link.getAttribute('href')).toBe(
expect(link!.getAttribute('href')).toBe(
'https://github.com/algolia/docsearch/issues/new?title=q'
);
});
Expand Down