Repository: wagtail/wagtail
File: client/src/controllers/PreviewController.test.js (JavaScript)

Code before:
    // Should create a new invisible iframe with the correct URL
    let iframes = document.querySelectorAll('iframe');
    expect(iframes.length).toEqual(2);
    const oldIframe = iframes[0];
    const newIframe = iframes[1];
    const oldIframeId = oldIframe.id;
    expect(oldIframeId).toBeTruthy();
    expect(newIframe.hasAttribute('id')).toBe(false);
    expect(newIframe.src).toEqual(expectedUrl);
    expect(newIframe.classList.contains('w-preview__proxy')).toBe(true);

    // Simulate the iframe loading
    const mockScroll = jest.fn();
    newIframe.contentWindow.scroll = mockScroll;
    await Promise.resolve();
    newIframe.dispatchEvent(new Event('load'));
    expect(mockScroll).toHaveBeenCalled();
    iframes = document.querySelectorAll('iframe');
    expect(iframes.length).toEqual(1);
    expect(iframes[0]).toBe(newIframe);
    expect(newIframe.id).toEqual(oldIframeId);
    expect(newIframe.src).toEqual(expectedUrl);
    expect(newIframe.getAttribute('style')).toBeNull();
    expect(newIframe.contentWindow.scroll).toHaveBeenCalledWith({
      top: oldIframe.contentWindow.scrollX,
      left: oldIframe.contentWindow.scrollY,
      behavior: 'instant',
    });

    // Clear the fetch call history
    fetch.mockClear();
  };

  const initializeOpenedPanel = async (expectedUrl) => {
    expect(global.fetch).not.toHaveBeenCalled();
    expect(events).toMatchObject({
      update: [],
      json: [],
      error: [],
      load: [],
      loaded: [],
      ready: [],
      updated: [],
    });

    application = Application.start();
    application.register(identifier, PreviewController);
    await Promise.resolve();

    // Should not have fetched the preview URL
    expect(global.fetch).not.toHaveBeenCalled();

Review comment (suggestion):
Oops this should've been
```suggestion
      top: oldIframe.contentWindow.scrollY,
      left: oldIframe.contentWindow.scrollX,
```

(and all the rest of the tests) 🤦 . They pass because the values are both `0`

Code after:
    // Should create a new invisible iframe with the correct URL
    let iframes = document.querySelectorAll('iframe');
    expect(iframes.length).toEqual(2);
    const oldIframe = iframes[0];
    const newIframe = iframes[1];
    const oldIframeId = oldIframe.id;
    expect(oldIframeId).toBeTruthy();
    expect(newIframe.hasAttribute('id')).toBe(false);
    expect(newIframe.src).toEqual(expectedUrl);
    expect(newIframe.classList.contains('w-preview__proxy')).toBe(true);

    // Pretend the old iframe has scrolled
    oldIframe.contentWindow.scrollX = 200;
    oldIframe.contentWindow.scrollY = 100;

    // Simulate the iframe loading
    const mockScroll = jest.fn();
    newIframe.contentWindow.scroll = mockScroll;
    await Promise.resolve();
    newIframe.dispatchEvent(new Event('load'));
    expect(mockScroll).toHaveBeenCalled();
    iframes = document.querySelectorAll('iframe');
    expect(iframes.length).toEqual(1);
    expect(iframes[0]).toBe(newIframe);
    expect(newIframe.id).toEqual(oldIframeId);
    expect(newIframe.src).toEqual(expectedUrl);
    expect(newIframe.getAttribute('style')).toBeNull();
    expect(newIframe.contentWindow.scroll).toHaveBeenCalledWith({
      top: oldIframe.contentWindow.scrollY,
      left: oldIframe.contentWindow.scrollX,
      behavior: 'instant',
    });

    // Clear the fetch call history
    fetch.mockClear();
  };

  const initializeOpenedPanel = async (expectedUrl) => {
    expect(global.fetch).not.toHaveBeenCalled();
    expect(events).toMatchObject({
      update: [],
      json: [],
      error: [],
      load: [],
      loaded: [],
      ready: [],
      updated: [],
    });

    application = Application.start();
    application.register(identifier, PreviewController);