I'm currently working on integrating an API found at https://developer.mozilla.org/en-US/docs/Web/API/Performance/measureUserAgentSpecificMemory. The code I am using has a simplified class structure.
export class Memory {
private stopped = false
private isUserAgentSpecificMemorySupported = true
public memoryData: any = []
constructor() {}
public startMonitoring(): () => void {
if (this.isUserAgentSpecificMemorySupported) {
this.scheduleMeasurement()
}
return () => {
this.stopped = true
}
}
private async performMeasurement(): Promise<void> {
const memory = await (window.performance as any).measureUserAgentSpecificMemory()
const type = memory.breakdown.filter((e: any) => e.types.includes('JavaScript'))
this.memoryData.push(type[0].bytes)
}
}
This relates to testing with Jest.
import {Memory} from './memory'
type UserAgentSpecificMemoryBreakdown = {
bytes: number
types: Array<string>
}
type UserAgentSpecificMemory = {
bytes: number
breakdown: Array<UserAgentSpecificMemoryBreakdown>
}
type MockWindow = {
crossOriginIsolated?: boolean
performance: {
measureUserAgentSpecificMemory?: () => Promise<UserAgentSpecificMemory>
}
}
const data = {
bytes: 1500,
breakdown: [
{
bytes: 1000000,
types: ['JavaScript'],
},
{
bytes: 0,
types: ['DOM'],
},
],
}
describe('Test Memory Class', () => {
let mockWindow: MockWindow
let windowSpy: jest.SpyInstance
beforeEach(() => {
windowSpy = jest.spyOn(window, 'window', 'get')
mockWindow = {
...window,
performance: {
measureUserAgentSpecificMemory: jest.fn(() => Promise.resolve(data)),
},
}
windowSpy.mockImplementation(() => mockWindow)
})
afterEach(() => {
windowSpy.mockRestore()
})
it('should measure User Agent Specific Memory', async () => {
let memory = new Memory()
memory.startMonitoring()
expect(memory.memoryData).toEqual([1000000])
})
})
I'm seeking guidance on how to implement asynchronous handling in the test script.
Your assistance is highly appreciated.