import { currentURL, click, waitFor } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { module, test } from 'qunit';

import window from 'ember-window-mock';
import { setupWindowMock } from 'ember-window-mock/test-support';

import setupMirage from '../helpers/setup-mirage';
import { visit } from '../helpers/visit-ignoring-abort';

module('Bug #2329', function (hooks) {
  setupApplicationTest(hooks);
  setupWindowMock(hooks);
  setupMirage(hooks);

  test('is fixed', async function (assert) {
    let user = {
      id: 42,
      login: 'johnnydee',
      email_verified: true,
      email_verification_sent: true,
      name: 'John Doe',
      email: 'john@doe.com',
      avatar: 'https://avatars2.githubusercontent.com/u/1234567?v=4',
      url: 'https://github.com/johnnydee',
    };

    this.server.get('/api/v1/me', {
      user,
      owned_crates: [
        { id: 123, name: 'foo-bar', email_notifications: true },
        { id: 56456, name: 'barrrrr', email_notifications: false },
      ],
    });

    this.server.get('/api/v1/me/tokens', { api_tokens: [] });

    let fakeWindow = {};
    window.open = () => fakeWindow;

    // 1. Sign out.
    window.localStorage.removeItem('isLoggedIn');

    // 2. Open the network debug panel.
    // ...

    // 3. Refresh the page.
    await visit('/');

    // 4. Click the "Log in with GitHub" link.
    await click('[data-test-login-link]');

    // 5. Complete the authentication workflow if necessary.

    // simulate the response from the `github-authorize` route
    window.postMessage({ ok: true, data: { user } }, window.location.origin);

    // wait for the user menu to show up after the successful login
    await waitFor('[data-test-user-menu]');

    // 6. Use the menu to navigate to "Account Settings".
    await click('[data-test-user-menu]');
    await click('[data-test-me-link]');
    await visit('/me');
    assert.equal(currentURL(), '/me');

    // 7. Observe that there are no crates listed under "Email Notification Preferences".
    // Observe that no request was sent to the /api/v1/me endpoint to obtain this data.

    // here we divert from the reproduction instructions, since this is the
    // issue that we want to fix
    assert.dom('[data-test-owned-crate]').exists({ count: 2 });
  });
});

@@ -549,469 +549,385 @@
 r = 
-%7B%0A      id: 42,%0A      login: 'johnnydee',%0A      email_verified: true,%0A      email_verification_sent: true,%0A      name: 'John Doe',%0A      email: 'john@doe.com',%0A      avatar: 'https://avatars2.githubusercontent.com/u/1234567?v=4',%0A      url: 'https://github.com/johnnydee',%0A    %7D;%0A%0A    this.server.get('/api/v1/me', %7B%0A      user,%0A      owned_
+this.server.create('user');%0A%0A    let foobar = this.server.create('crate', %7B name: 'foo-bar' %7D);%0A    this.server.create('crate-ownership', %7B crate: foobar, user, emailNotifications: true %7D);%0A    this.server.create('version', %7B crate: foobar %7D);%0A%0A    let bar = this.server.
 cr
+e
 ate
-s: %5B%0A        %7B id: 123, name: 'foo-bar', email_notifications: true %7D,%0A        %7B id: 56456, nam
+('crate', %7B name: 'barrrrr' %7D);%0A    this.server.create('crate-ownership', %7B crat
 e: 
-'
 bar
-rrrr'
+, user
 , email
-_n
+N
 otif
@@ -946,29 +946,12 @@
 lse 
-%7D,%0A      %5D,%0A    
 %7D);%0A
-%0A
     
@@ -966,49 +966,38 @@
 ver.
-get('/api/v1/me/tokens', %7B api_tokens: %5B%5D
+create('version', %7B crate: bar
  %7D);
@@ -1387,16 +1387,110 @@
 ssary.%0A%0A
+    // simulate login on the mirage side%0A    this.server.create('mirage-session', %7B user %7D);%0A%0A
     // s
@@ -1581,24 +1581,8 @@
 true
-, data: %7B user %7D
  %7D, 

Use mirage setup for fixtures