How to make the window object to pass on jest

at the moment my dfxContent is undefined which is the window.global

declare global {
  interface Window {
    dfxContent: Record<string, string>
  }
}

export interface ReplaceContentModel {
  config_key: string
}

export const replaceContent = ({
  config_key,
}: ReplaceContentModel): string | Error => {
  const message = `No content exists for ${config_key}`
  if (window.dfxContent[config_key]) {
    return window.dfxContent[config_key]
  }
  return new Error(message)
}

my tests file

describe('Given a replaceContent util function', () => {
  test('window object', () => {
    const originalWindow = global.window
    originalWindow.dfxContent = { config_key: 'Hi' }
    const windowSpy = jest.spyOn(global, 'global', 'get')
    windowSpy.mockImplementation(() => ({
      ...originalWindow,
    }))

    expect(originalWindow).toBe('Hi')
  })
})

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.