https://github.com/crowdstrike/ember-browser-services
Services for interacting with browser APIs so that you can have fine-grained control in tests.
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (9.6%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Services for interacting with browser APIs so that you can have fine-grained control in tests.
Basic Info
Statistics
- Stars: 47
- Watchers: 11
- Forks: 9
- Open Issues: 16
- Releases: 33
Topics
Metadata Files
README.md
ember-browser-services
ember-browser-services is a collection of Ember Services that allow for
consistent interaction with browser APIs.
When all browser APIs are accessed via services, browser behavior is now stubbable in unit tests!
This addon is written in TypeScript so that your editor will provide intellisense hints to guide you through usage so that you don't have to spend as much time looking at the documentation.
Installation
pnpm add ember-browser-services
# or
yarn add ember-browser-services
# or
npm install ember-browser-services
# or
ember install ember-browser-services
Compatibility
- Ember.js v4.8 or above
- ember-auto-import v2 or above
- typescript v4.5 or above
- embroider max-compat and strict modes
Usage
Whenever you would reach for window, or any other browser API, inject the
service instead.
```js export default class MyComponent extends Component { @service('browser/window') window;
@action externalRedirect() { this.window.location.href = 'https://crowdstrike.com'; } } ```
Testing
for fuller examples, see the tests directory
There are two types of stubbing you may be interested in when working with browser services - service overriding
As with any service, if the default implementation is not suitable for testing,
it may be swapped out during the test.
```js
import Service from '@ember/service';
module('Scenario Name', function (hooks) {
test('rare browser API', function (assert) {
let called = false;
this.owner.register(
'service:browser/window',
class TestWindow extends Service {
rareBrowserApi() {
called = true;
}
},
);
this.owner.lookup('service:browser/window').rareBrowserApi();
assert.ok(called, 'the browser api was called');
});
});
```
- direct assignment
This approach may be useful for deep-objects are complex interactions that otherwise would be hard to reproduce via normal UI interaction.
```js module('Scenario Name', function (hooks) { test('rare browser API', function (assert) { let service = this.owner.lookup('service:browser/window'); let called = false;
service.rareBrowserApi = () => (called = true);
service.rareBrowserApi();
assert.ok(called, 'the browser api was called');
});
}); ```
There is also a shorthand for grouped "modules" in your tests:
Window
```js import { setupBrowserFakes } from 'ember-browser-services/test-support';
module('Scenario Name', function (hooks) { setupBrowserFakes(hooks, { window: true });
test('is at crowdstrike.com', function (assert) { let service = this.owner.lookup('service:browser/window');
// somewhere in a component or route or service
// windowService.location = '/';
assert.equal(service.location.href, '/'); // => succeeds
}); }); ```
Alternatively, specific APIs of the window can be stubbed with an object
```js import { setupBrowserFakes } from 'ember-browser-services/test-support';
module('Scenario Name', function (hooks) { setupBrowserFakes(hooks, { window: { location: { href: 'https://crowdstrike.com' } }, });
test('is at crowdstrike.com', function (assert) { let service = this.owner.lookup('service:browser/window');
assert.equal(service.location.href, 'https://crowdstrike.com'); // => succeeds
}); }); ```
localStorage
```js import { setupBrowserFakes } from 'ember-browser-services/test-support';
module('Scenario Name', function (hooks) { setupBrowserFakes(hooks, { localStorage: true });
test('local storage service works', function (assert) { let service = this.owner.lookup('service:browser/local-storage');
assert.equal(service.getItem('foo'), null);
service.setItem('foo', 'bar');
assert.equal(service.getItem('foo'), 'bar');
assert.equal(localStorage.getItem('foo'), null);
}); }); ```
sessionStorage
```js import { setupBrowserFakes } from 'ember-browser-services/test-support';
module('Scenario Name', function (hooks) { setupBrowserFakes(hooks, { sessionStorage: true });
test('session storage service works', function (assert) { let service = this.owner.lookup('service:browser/session-storage');
assert.equal(service.getItem('foo'), null);
service.setItem('foo', 'bar');
assert.equal(service.getItem('foo'), 'bar');
assert.equal(sessionStorage.getItem('foo'), null);
}); }); ```
navigator
```js // An example test from ember-jsqr's tests module('Scenario Name', function (hooks) { setupApplicationTest(hooks); setupBrowserFakes(hooks, { navigator: { mediaDevices: { getUserMedia: () => ({ getTracks: () => [] }), }, }, });
test('the camera can be turned on and then off', async function (assert) { let selector = '[data-test-single-camera-demo] button';
await visit('/docs/single-camera');
await click(selector);
assert.dom(selector).hasText('Stop Camera', 'the camera is now on');
await click(selector);
assert.dom(selector).hasText('Start Camera', 'the camera has been turned off');
}); }); ```
document
```js import { setupBrowserFakes } from 'ember-browser-services/test-support';
module('Examples: How to use the browser/document service', function (hooks) { setupBrowserFakes(hooks, { document: { title: 'Foo', }, });
test('title interacts separately from the real document', function (assert) { let service = this.owner.lookup('service:browser/document');
assert.equal(service.title, 'Foo');
assert.notEqual(service.title, document.title);
service.title = 'Bar';
assert.equal(service.title, 'Bar');
assert.notEqual(service.title, document.title);
}); }); ```
What about ember-window-mock?
ember-window-mock offers much of the same feature set as ember-browser-services.
ember-browser-services builds on top of ember-window-mock and the two libraries can be used together.
The main differences being:
- ember-window-mock
- smaller API surface
- uses imports for window instead of a service
- all browser APIs must be accessed from the imported window to be mocked / stubbed
- adding additional behavior to the test version of an object requires something like:
```js
import window from 'ember-window-mock';
// ....
window.location = new TestLocation();
window.parent.location = window.location;
```
ember-browser-services
- uses services instead of imports
- multiple top-level browser APIs, instead of just
window - setting behavior on services can be done by simply assigning, thanks to ember-window-mock
```js let service = this.owner.lookup('service:browser/navigator');
service.someApi = someValue; ``` - or adding additional behavior to the test version of an object can be done via familiar service extension like:
```js this.owner.register( 'service:browser/window', class extends Service { location = new TestLocation();
parent = this; },); ``
- because of the ability to register custom services during tests, if app authors want to customize their own implementation of test services, that can be done without a PR to the addon - there is an object short-hand notation for customizing browser APIs viasetupBrowserFakes` (demonstrated in the above examples)
Similarities / both addons:
- use proxies to fallback to default browser API behavior
- provide default stubs for commonly tested behavior (location, localStorage)
- all state reset between tests
Contributing
See the Contributing guide for details.
License
This project is licensed under the MIT License.
Owner
- Name: CrowdStrike
- Login: CrowdStrike
- Kind: organization
- Email: github@crowdstrike.com
- Location: United States of America
- Website: https://www.crowdstrike.com
- Repositories: 183
- Profile: https://github.com/CrowdStrike
GitHub Events
Total
- Create event: 31
- Release event: 1
- Issues event: 1
- Delete event: 25
- Issue comment event: 41
- Push event: 227
- Pull request event: 58
- Pull request review event: 66
Last Year
- Create event: 31
- Release event: 1
- Issues event: 1
- Delete event: 25
- Issue comment event: 41
- Push event: 227
- Pull request event: 58
- Pull request review event: 66
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Renovate Bot | b****t@r****m | 242 |
| renovate[bot] | 2****] | 124 |
| NullVoxPopuli | L****v@g****m | 111 |
| semantic-release-bot | s****t@m****t | 30 |
| Simon Ihmig | s****g@g****m | 11 |
| Drew Neil | a****l@g****m | 5 |
| github-actions[bot] | g****] | 3 |
| dependabot[bot] | 4****] | 2 |
| Tomster | t****r@e****m | 1 |
| MrChocolatine | 4****e | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 4
- Total pull requests: 164
- Average time to close issues: 24 days
- Average time to close pull requests: about 1 month
- Total issue authors: 4
- Total pull request authors: 6
- Average comments per issue: 3.25
- Average comments per pull request: 0.66
- Merged pull requests: 134
- Bot issues: 1
- Bot pull requests: 153
Past Year
- Issues: 2
- Pull requests: 51
- Average time to close issues: about 1 month
- Average time to close pull requests: 9 days
- Issue authors: 2
- Pull request authors: 3
- Average comments per issue: 3.5
- Average comments per pull request: 1.22
- Merged pull requests: 37
- Bot issues: 0
- Bot pull requests: 48
Top Authors
Issue Authors
- simonihmig (2)
- nelstrom (1)
- renovate[bot] (1)
- elwayman02 (1)
Pull Request Authors
- renovate[bot] (203)
- simonihmig (8)
- NullVoxPopuli (6)
- github-actions[bot] (4)
- MrChocolatine (2)
- dependabot[bot] (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- npm 31,727 last-month
- Total dependent packages: 5
- Total dependent repositories: 11
- Total versions: 33
- Total maintainers: 3
npmjs.org: ember-browser-services
Browser APIs as services for easier testing
- Homepage: https://github.com/CrowdStrike/ember-browser-services#readme
- License: MIT
-
Latest release: 5.0.1
published over 1 year ago
Rankings
Maintainers (3)
Dependencies
- actions/upload-artifact v3 composite
- actions/download-artifact v3 composite
- actions/setup-node v3 composite
- pnpm/action-setup v2.2.2 composite
- ./.github/actions/assert-build * composite
- ./.github/actions/download-built-package * composite
- ./.github/actions/pnpm * composite
- actions/checkout v3 composite
- wagoid/commitlint-github-action v5.2.0 composite
- @babel/core 7.18.10 development
- @babel/plugin-proposal-class-properties 7.18.6 development
- @babel/plugin-proposal-decorators 7.18.10 development
- @babel/plugin-syntax-decorators 7.18.6 development
- @babel/plugin-transform-typescript 7.18.12 development
- @babel/preset-typescript 7.18.6 development
- @embroider/addon-dev 1.8.3 development
- @nullvoxpopuli/eslint-configs 2.2.47 development
- @semantic-release/changelog ^6.0.1 development
- @semantic-release/git ^10.0.1 development
- @types/ember__application ^4.0.0 development
- @types/ember__engine ^4.0.0 development
- @types/ember__object ^4.0.0 development
- @types/ember__service ^4.0.0 development
- @types/ember__test-helpers ^2.0.2 development
- @types/qunit ^2.11.3 development
- babel-eslint 10.1.0 development
- concurrently 7.3.0 development
- eslint ^7.0.0 development
- eslint-config-prettier 8.5.0 development
- eslint-plugin-decorator-position 4.0.1 development
- eslint-plugin-ember 10.6.1 development
- eslint-plugin-import 2.26.0 development
- eslint-plugin-json 3.1.0 development
- eslint-plugin-node 11.1.0 development
- eslint-plugin-prettier 4.2.1 development
- eslint-plugin-simple-import-sort 7.0.0 development
- prettier ^2.2.1 development
- rollup 2.77.2 development
- rollup-plugin-ts 2.0.7 development
- semantic-release ^19.0.3 development
- typescript 4.7.4 development
- @embroider/addon-shim ^1.3.0
- ember-window-mock ^0.8.1
- prettier ^2.2.1 development
- 1843 dependencies
- @babel/core 7.18.10 development
- @commitlint/cli ^16.2.1 development
- @commitlint/config-conventional ^16.2.1 development
- @ember/optional-features ^2.0.0 development
- @ember/test-helpers ^2.8.1 development
- @embroider/test-setup 1.8.3 development
- @glimmer/component ^1.1.2 development
- @glimmer/tracking ^1.1.2 development
- @nullvoxpopuli/eslint-configs ^2.2.2 development
- @semantic-release/changelog ^6.0.1 development
- @semantic-release/git ^10.0.1 development
- @types/ember ^4.0.0 development
- @types/ember-qunit ^5.0.0 development
- @types/ember-resolver ^5.0.11 development
- @types/ember__application ^4.0.0 development
- @types/ember__array ^4.0.1 development
- @types/ember__component ^4.0.8 development
- @types/ember__controller ^4.0.0 development
- @types/ember__debug ^4.0.1 development
- @types/ember__engine ^4.0.0 development
- @types/ember__error ^4.0.0 development
- @types/ember__object ^4.0.2 development
- @types/ember__polyfills ^4.0.0 development
- @types/ember__routing ^4.0.7 development
- @types/ember__runloop ^4.0.1 development
- @types/ember__service ^4.0.0 development
- @types/ember__string ^3.0.9 development
- @types/ember__template ^4.0.0 development
- @types/ember__test ^4.0.0 development
- @types/ember__test-helpers ^2.6.1 development
- @types/ember__utils ^4.0.0 development
- @types/htmlbars-inline-precompile ^3.0.0 development
- @types/qunit ^2.19.0 development
- @types/rsvp ^4.0.4 development
- @typescript-eslint/eslint-plugin 5.33.0 development
- @typescript-eslint/parser ^5.12.1 development
- babel-eslint ^10.1.0 development
- broccoli-asset-rev ^3.0.0 development
- ember-auto-import ^2.4.2 development
- ember-cli ~4.6.0 development
- ember-cli-app-version ^5.0.0 development
- ember-cli-babel ^7.26.3 development
- ember-cli-dependency-checker ^3.3.1 development
- ember-cli-htmlbars ^6.0.1 development
- ember-cli-inject-live-reload ^2.0.2 development
- ember-cli-sri ^2.1.1 development
- ember-cli-terser ^4.0.1 development
- ember-cli-typescript ^5.1.0 development
- ember-cli-typescript-blueprints ^3.0.0 development
- ember-disable-prototype-extensions ^1.1.3 development
- ember-export-application-global ^2.0.1 development
- ember-load-initializers ^2.1.2 development
- ember-maybe-import-regenerator ^1.0.0 development
- ember-page-title ^7.0.0 development
- ember-qunit ^5.1.4 development
- ember-resolver ^8.0.2 development
- ember-source ~4.6.0 development
- ember-source-channel-url ^3.0.0 development
- ember-template-lint ^3.2.0 development
- ember-try ^2.0.0 development
- eslint ^7.23.0 development
- eslint-config-prettier ^8.1.0 development
- eslint-plugin-decorator-position ^4.0.1 development
- eslint-plugin-ember ^10.3.0 development
- eslint-plugin-markdown ^2.2.1 development
- eslint-plugin-node ^11.1.0 development
- eslint-plugin-prettier ^3.3.1 development
- eslint-plugin-qunit ^7.2.0 development
- loader.js ^4.7.0 development
- npm-run-all ^4.1.5 development
- prettier ^2.2.1 development
- qunit ^2.19.1 development
- qunit-console-grouper ^0.3.0 development
- qunit-dom ^2.0.0 development
- remark-cli ^10.0.1 development
- remark-lint ^9.1.1 development
- remark-preset-lint-recommended ^6.1.2 development
- semantic-release ^19.0.2 development
- typescript ^4.7.3 development
- webpack ^5.70.0 development
- ember-browser-services *