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

import percySnapshot from '@percy/ember';
import a11yAudit from 'ember-a11y-testing/test-support/audit';

import axeConfig from '../axe-config';
import setupMirage from '../helpers/setup-mirage';

module('Acceptance | categories', function (hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);

  test('listing categories', async function (assert) {
    this.server.create('category', { category: 'API bindings' });
    this.server.create('category', { category: 'Algorithms' });
    this.server.createList('crate', 1, { categoryIds: ['algorithms'] });
    this.server.create('category', { category: 'Asynchronous' });
    this.server.createList('crate', 15, { categoryIds: ['asynchronous'] });

    await visit('/categories');

    assert.dom('[data-test-category="api-bindings"] [data-test-crate-count]').hasText('0 crates');
    assert.dom('[data-test-category="algorithms"] [data-test-crate-count]').hasText('1 crate');
    assert.dom('[data-test-category="asynchronous"] [data-test-crate-count]').hasText('15 crates');

    await percySnapshot(assert);
    await a11yAudit(axeConfig);
  });

  test('category/:category_id index default sort is recent-downloads', async function (assert) {
    this.server.create('category', { category: 'Algorithms' });

    await visit('/categories/algorithms');

    assert.dom('[data-test-category-sort] [data-test-current-order]').hasText('Recent Downloads');

    await percySnapshot(assert);
    await a11yAudit(axeConfig);
  });

  test('listing category slugs', async function (assert) {
    this.server.create('category', { category: 'Algorithms', description: 'Crates for algorithms' });
    this.server.create('category', { category: 'Asynchronous', description: 'Async crates' });

    await visit('/category_slugs');

    assert.dom('[data-test-category-slug="algorithms"]').hasText('algorithms');
    assert.dom('[data-test-category-description="algorithms"]').hasText('Crates for algorithms');
    assert.dom('[data-test-category-slug="asynchronous"]').hasText('asynchronous');
    assert.dom('[data-test-category-description="asynchronous"]').hasText('Async crates');
  });
});

@@ -229,24 +229,130 @@
 port/audit';
+%0Aimport window from 'ember-window-mock';%0Aimport %7B setupWindowMock %7D from 'ember-window-mock/test-support';
 %0A%0Aimport axe
@@ -516,16 +516,42 @@
 hooks);%0A
+  setupWindowMock(hooks);%0A
   setupM
@@ -612,32 +612,79 @@
 tion (assert) %7B%0A
+    window.navigator = %7B languages: %5B'en'%5D %7D;%0A%0A
     this.server.
@@ -1007,24 +1007,106 @@
 ronous'%5D %7D);
+%0A    this.server.create('category', %7B category: 'Everything', crates_cnt: 1234 %7D);
 %0A%0A    await 
@@ -1427,71 +1427,510 @@
 ');%0A
-%0A    await percySnapshot(assert);%0A    await a11yAudit(axeConfig
+    assert.dom('%5Bdata-test-category=%22everything%22%5D %5Bdata-test-crate-count%5D').hasText('1,234 crates');%0A%0A    await percySnapshot(assert);%0A    await a11yAudit(axeConfig);%0A  %7D);%0A%0A  test('listing categories (locale: de)', async function (assert) %7B%0A    window.navigator = %7B languages: %5B'de'%5D %7D;%0A%0A    this.server.create('category', %7B category: 'Everything', crates_cnt: 1234 %7D);%0A%0A    await visit('/categories');%0A    assert.dom('%5Bdata-test-category=%22everything%22%5D %5Bdata-test-crate-count%5D').hasText('1.234 crates'
 );%0A 

Add checks for large `crates_cnt` values