Skip to content

[Feature Request] Support stories that override the user agent #302

Open
@notclive

Description

@notclive

I have a component that behaves differently on different browsers / operating systems.
I want to test these different behaviours.

I use a decorator that simulates the different environments by overridding the user agent:
export function givenUserAgent (userAgent: string) {
  function overrideUserAgent (userAgent: string) {
    Object.defineProperty(window.navigator, 'userAgent', {
      get: () => userAgent,
      configurable: true
    })
  }

  return function SetUserAgent (Story: StoryFn<any>) {
    const [isReady, setIsReady] = useReducer(() => true, false)

    useEffect(() => {
      const initialUserAgent = window.navigator.userAgent
      overrideUserAgent(userAgent)
      setIsReady()
      return function restoreUserAgent () {
        overrideUserAgent(initialUserAgent)
      }
    }, [])

    return <>
      {isReady && <Story />}
    </>
  }
}

But test-runner overrides the user agent itself in setup-page.ts

function addToUserAgent(extra) {

Because it does so without setting configurable: true when my decorator tries to override the user agent I get an error.

Message:
     Cannot redefine property: userAgent

      at __throwError (<anonymous>:180:15)
      at eval (eval at evaluate (:191:30), <anonymous>:4:19)
      at UtilityScript.evaluate (<anonymous>:193:17)
      at UtilityScript.<anonymous> (<anonymous>:1:44)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions