fingerprintjs
103 строки · 2.8 Кб
1import { withMockProperties } from '../../tests/utils'
2import { getWebGlBasics, getWebGlExtensions, STATUS_GET_PARAMETER_NOT_A_FUNCTION, STATUS_NO_GL_CONTEXT } from './webgl'
3
4function isWebGLSupported() {
5const canvas = document.createElement('canvas')
6const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
7// Report the result.
8return gl && gl instanceof WebGLRenderingContext
9}
10
11describe('Sources', () => {
12const options = {
13cache: {},
14}
15
16function withMissingGetParameter(action: () => void) {
17return withMockProperties(
18document,
19{ createElement: { value: () => ({ getContext: () => ({}), addEventListener: () => undefined }) } },
20action,
21)
22}
23
24describe('webGlBasics', () => {
25it('should work', () => {
26if (!isWebGLSupported()) {
27expect(getWebGlBasics(options)).toBe(STATUS_NO_GL_CONTEXT)
28return
29}
30
31const result = getWebGlBasics(options)
32
33// Type guard
34if (typeof result === 'number') {
35throw new Error('WebGL is missing')
36}
37
38expect(result.vendor.length).toBeGreaterThan(0)
39expect(result.version.toLocaleLowerCase()).toContain('webgl')
40})
41
42it('returns corresponding status when `context.getParameter` is absent', async () => {
43await withMissingGetParameter(() => {
44expect(
45getWebGlBasics({
46cache: {},
47}),
48).toBe(STATUS_GET_PARAMETER_NOT_A_FUNCTION)
49})
50})
51
52it('is stable', () => {
53const first = getWebGlBasics({ cache: {} })
54const second = getWebGlBasics({ cache: {} })
55
56expect(first).toEqual(second)
57})
58})
59
60describe('webGlExtensions', () => {
61it('should work', () => {
62if (!isWebGLSupported()) {
63expect(getWebGlExtensions(options)).toBe(STATUS_NO_GL_CONTEXT)
64return
65}
66
67const result = getWebGlExtensions(options)
68
69// Type guard
70if (typeof result === 'number') {
71throw new Error('WebGL is missing')
72}
73
74for (const [key, value] of Object.entries(result)) {
75if (value === null) {
76expect(key).toBe('extensions')
77} else {
78expect(value).withContext(key).toBeInstanceOf(Array)
79for (let i = 0; i < value.length; ++i) {
80expect(value[i]).withContext(`${key}[${i}]`).toBeInstanceOf(String)
81}
82}
83}
84})
85
86it('returns corresponding status when `context.getParameter` is absent', async () => {
87await withMissingGetParameter(() => {
88expect(
89getWebGlExtensions({
90cache: {},
91}),
92).toBe(STATUS_GET_PARAMETER_NOT_A_FUNCTION)
93})
94})
95
96it('is stable', () => {
97const first = getWebGlExtensions({ cache: {} })
98const second = getWebGlExtensions({ cache: {} })
99
100expect(first).toEqual(second)
101})
102})
103})
104