|
| 1 | +/** |
| 2 | + * vitest가 JSDOM을 사용하여 브라우저 환경을 시뮬레이션하도록 설정합니다. |
| 3 | + * 이를 통해 테스트 환경에서 window, navigator 등의 객체를 사용할 수 있습니다. |
| 4 | + * |
| 5 | + * @vitest-environment jsdom |
| 6 | + */ |
| 7 | +import { describe, test, expect, afterEach } from "vitest"; |
| 8 | +import getDevice from "."; |
| 9 | + |
| 10 | +describe("getDevice", () => { |
| 11 | + // userAgent를 테스트 케이스마다 다르게 설정하기 위한 헬퍼 함수 |
| 12 | + const mockUserAgent = (userAgent: string) => { |
| 13 | + // JSDOM이 생성한 window.navigator 객체의 userAgent 속성을 덮어씁니다. |
| 14 | + Object.defineProperty(window.navigator, "userAgent", { |
| 15 | + value: userAgent, |
| 16 | + writable: true, |
| 17 | + configurable: true, |
| 18 | + }); |
| 19 | + }; |
| 20 | + |
| 21 | + // 각 테스트가 끝난 후, 다음 테스트에 영향을 주지 않도록 userAgent를 초기화합니다. |
| 22 | + afterEach(() => { |
| 23 | + mockUserAgent(""); |
| 24 | + }); |
| 25 | + |
| 26 | + describe("클라이언트 (브라우저) 환경", () => { |
| 27 | + describe("데스크톱", () => { |
| 28 | + test("Windows Chrome User Agent를 데스크톱으로 인식해야 합니다", () => { |
| 29 | + mockUserAgent( |
| 30 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" |
| 31 | + ); |
| 32 | + const { isDesktop, isMobile, isTablet } = getDevice(); |
| 33 | + expect(isDesktop).toBe(true); |
| 34 | + expect(isMobile).toBe(false); |
| 35 | + expect(isTablet).toBe(false); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("모바일", () => { |
| 40 | + test("iPhone User Agent를 모바일 및 iOS로 인식해야 합니다", () => { |
| 41 | + mockUserAgent( |
| 42 | + "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148" |
| 43 | + ); |
| 44 | + const { isMobile, isIOS, isDesktop } = getDevice(); |
| 45 | + expect(isMobile).toBe(true); |
| 46 | + expect(isIOS).toBe(true); |
| 47 | + expect(isDesktop).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + test("Android Phone User Agent를 모바일 및 Android로 인식해야 합니다", () => { |
| 51 | + mockUserAgent( |
| 52 | + "Mozilla/5.0 (Linux; Android 13; SM-S908B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36" |
| 53 | + ); |
| 54 | + const { isMobile, isAndroid, isTablet } = getDevice(); |
| 55 | + expect(isMobile).toBe(true); |
| 56 | + expect(isAndroid).toBe(true); |
| 57 | + expect(isTablet).toBe(false); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("태블릿", () => { |
| 62 | + test("iPad User Agent를 태블릿 및 iOS로 인식해야 합니다", () => { |
| 63 | + mockUserAgent( |
| 64 | + "Mozilla/5.0 (iPad; CPU OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1" |
| 65 | + ); |
| 66 | + const { isTablet, isIOS, isMobile } = getDevice(); |
| 67 | + expect(isTablet).toBe(true); |
| 68 | + expect(isIOS).toBe(true); |
| 69 | + expect(isMobile).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + test("Android Tablet User Agent를 태블릿 및 Android로 인식해야 합니다", () => { |
| 73 | + mockUserAgent( |
| 74 | + "Mozilla/5.0 (Linux; Android 12; SM-X906C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.99 Safari/537.36" |
| 75 | + ); |
| 76 | + const { isTablet, isAndroid, isMobile } = getDevice(); |
| 77 | + expect(isTablet).toBe(true); |
| 78 | + expect(isAndroid).toBe(true); |
| 79 | + expect(isMobile).toBe(false); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments