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: get screen and window info #7

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './browser-info';
export * from './web-capabilities';
export * from './cpu-info';
export * from './window-info';
48 changes: 48 additions & 0 deletions src/window-info.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { windowInfo } from './window-info';

describe('windowInfo', () => {
it('returns the correct screen width and height when accessed', () => {
expect.hasAssertions();

jest.spyOn(window.screen, 'width', 'get').mockReturnValue(1280);
jest.spyOn(window.screen, 'height', 'get').mockReturnValue(720);

expect(windowInfo.getScreenWidth()).toBe(1280);
expect(windowInfo.getScreenHeight()).toBe(720);
});
it('returns undefined for screen width and height when the properties are not defined', () => {
expect.hasAssertions();

jest.spyOn(window.screen, 'height', 'get').mockImplementation();
jest.spyOn(window.screen, 'width', 'get').mockImplementation();

expect(windowInfo.getScreenWidth()).toBeUndefined();
expect(windowInfo.getScreenHeight()).toBeUndefined();
});
it('returns the correct window width and height when accessed', () => {
expect.hasAssertions();

Object.defineProperty(window, 'innerWidth', {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

innerWidth and innerHeight do not have a get property, so they couldn't be spied on.

value: 1080,
});
Object.defineProperty(window, 'innerHeight', {
value: 480,
});

expect(windowInfo.getWindowWidth()).toBe(1080);
expect(windowInfo.getWindowHeight()).toBe(480);
});
it('returns undefined for window width and height when the properties are not defined', () => {
expect.hasAssertions();

Object.defineProperty(window, 'innerWidth', {
value: undefined,
});
Object.defineProperty(window, 'innerHeight', {
value: undefined,
});

expect(windowInfo.getWindowWidth()).toBeUndefined();
expect(windowInfo.getWindowHeight()).toBeUndefined();
});
});
40 changes: 40 additions & 0 deletions src/window-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Provides information about the screen dimensions.
*/
export class windowInfo {
/**
* Retrieves the width of the screen.
*
* @returns The width of the screen in pixels, or undefined if not available.
*/
static getScreenWidth(): number | undefined {
return window.screen.width;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

window.screen.width and screen.width will produce the same result. Both properties access the width property of the global screen object.

though, when using only screen.width we get the following eslint problem // eslint-disable-next-line no-restricted-globals

}

/**
* Retrieves the height of the screen.
*
* @returns The height of the screen in pixels, or undefined if not available.
*/
static getScreenHeight(): number | undefined {
return window.screen.height;
}

/**
* Retrieves the width of the browser window's content area.
*
* @returns The width of the content area in pixels, or undefined if not available.
*/
static getWindowWidth(): number | undefined {
return window.innerWidth;
}

/**
* Retrieves the height of the browser window's content area.
*
* @returns The height of the content area in pixels, or undefined if not available.
*/
static getWindowHeight(): number | undefined {
return window.innerHeight;
}
}
Loading