Skip to content

test(@toss/utils): wrote test code for getViewportSize function #259

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 13 commits into from
May 31, 2024

Conversation

okinawaa
Copy link
Member

Overview

I wrote test code for getViewportSize function

PR Checklist

  • I read and included theses actions below
  1. I have read the Contributing Guide
  2. I have written documents and tests, if needed.

@netlify
Copy link

netlify bot commented Jun 14, 2023

👷 Deploy request for slash-libraries pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 6f45aa6

});

it('should return zero size when running on server', () => {
delete (global as { window: unknown }).window;
Copy link
Member Author

Choose a reason for hiding this comment

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

I'd like to test the server status, should I fill it out like this?
Is it better to mock the isServer function itself

Copy link
Contributor

@JayeHa JayeHa Nov 16, 2023

Choose a reason for hiding this comment

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

@okinawaa
I think directly chaning the window object in your tests could be risky. 😢
I'm not sure that your tests won't affect the other tests.

How about mocking it like this instead?
I try it based on this reference!

import { getViewportSize } from '.';

let windowSpy: jest.SpyInstance;

beforeEach(() => {
  windowSpy = jest.spyOn(global, 'window', 'get');
});
afterEach(() => {
  windowSpy.mockRestore();
});

describe('getViewportSize', () => {
  it('should return viewport size', () => {
    windowSpy.mockImplementation(() => ({
      innerWidth: 1024,
      innerHeight: 768,
    }));

    const expectedSize = { width: 1024, height: 768 };
    const size = getViewportSize();

    expect(size).toEqual(expectedSize);
  });

  it('should return zero size when running on server', () => {
    windowSpy.mockImplementation(() => undefined);

    const expectedSize = { width: 0, height: 0 };
    const size = getViewportSize();

    expect(size).toEqual(expectedSize);
  });
});

@okinawaa okinawaa enabled auto-merge (squash) July 23, 2023 09:14
@raon0211 raon0211 disabled auto-merge May 31, 2024 08:14
Copy link

vercel bot commented May 31, 2024

@raon0211 is attempting to deploy a commit to the Toss Team on Vercel.

A member of the Team first needs to authorize it.

@raon0211 raon0211 merged commit ccacb86 into toss:main May 31, 2024
0 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants