<commit_before>import { module, test, only } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, find } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Pretender from 'pretender';

module('Integration | Component | cloudinary-resource-list', function (hooks) {
  setupRenderingTest(hooks);

  test('it gracefully handles fetch error', async function (assert) {
    await render(hbs`
      {{#cloudinary-resource-list 'test' as |resourceList|}}
        it renders block content
      {{/cloudinary-resource-list}}
    `);

    assert.equal(this.element.textContent.trim(), 'it renders block content');
  });

  test('it renders cloudinary response in correct order', async function (assert) {
    let server = new Pretender();
    server.get('https://res.cloudinary.com/cloudinary-test/image/list/test.json', () => {
      let cloudinaryResourceListResponse = {
        'resources': [{
          public_id: 'my_project/image_b',
          version: 1509653786,
          format: 'jpg',
          width: 2100,
          height: 1400,
          type: 'upload',
          created_at: '2017-11-02T14:06:53Z',
          context: {
            custom: {
              caption: 'Image B',
              order: '02'
            }
          }
        }, {
          public_id: 'my_project/image_c',
          version: 1509653789,
          format: 'jpg',
          width: 2100,
          height: 1400,
          type: 'upload',
          created_at: '2017-11-02T14:06:53Z',
          context: {
            custom: {
              caption: 'Image C',
              order: '03'
            }
          }
        }, {
          public_id: 'my_project/image_a',
          version: 1509653787,
          format: 'jpg',
          width: 2100,
          height: 1400,
          type: 'upload',
          created_at: '2017-11-02T14:06:52Z',
          context: {
            custom: {
              caption: 'Image A',
              order: '01'
            }
          }
        }],
        updated_at: '2017-11-03T13:16:29Z'
      }
      return [200, {
        'Content-Type': 'application/json'
      }, JSON.stringify(cloudinaryResourceListResponse)]
    });

    await render(hbs`
      {{#cloudinary-resource-list 'test' as |resourceList|}}
        {{#each resourceList.items as |item|}}
          <span>{{item.public_id}}</span>
        {{/each}}
      {{/cloudinary-resource-list}}
    `);

    assert.ok(find('span').textContent.trim().endsWith('image_a'), 'Image A order is OK');
    assert.ok(find('span:nth-child(2)').textContent.trim().endsWith('image_b'), 'Image B order is OK');
    assert.ok(find('span:nth-child(3)').textContent.trim().endsWith('image_c'), 'Image C order is OK');
  });

  only('it fetches images without custom context', async function(assert) {
    let server = new Pretender();
    server.get('https://res.cloudinary.com/cloudinary-test/image/list/test.json', () => {
      let cloudinaryResourceListResponse = {
        resources: [
          {
            public_id: 'image_a',
            version: 1476216440,<commit_msg>shutdown pretender after each test<commit_after>import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, find } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Pretender from 'pretender';

module('Integration | Component | cloudinary-resource-list', function (hooks) {
  setupRenderingTest(hooks);

  hooks.beforeEach(function() {
    this.server = new Pretender();
  });

  hooks.afterEach(function() {
    this.server.shutdown();
  });

  test('it gracefully handles fetch error', async function (assert) {
    await render(hbs`
      {{#cloudinary-resource-list 'test' as |resourceList|}}
        it renders block content
      {{/cloudinary-resource-list}}
    `);

    assert.equal(this.element.textContent.trim(), 'it renders block content');
  });

  test('it renders cloudinary response in correct order', async function (assert) {
    this.server.get('https://res.cloudinary.com/cloudinary-test/image/list/test.json', () => {
      let cloudinaryResourceListResponse = {
        'resources': [{
          public_id: 'my_project/image_b',
          version: 1509653786,
          format: 'jpg',
          width: 2100,
          height: 1400,
          type: 'upload',
          created_at: '2017-11-02T14:06:53Z',
          context: {
            custom: {
              caption: 'Image B',
              order: '02'
            }
          }
        }, {
          public_id: 'my_project/image_c',
          version: 1509653789,
          format: 'jpg',
          width: 2100,
          height: 1400,
          type: 'upload',
          created_at: '2017-11-02T14:06:53Z',
          context: {
            custom: {
              caption: 'Image C',
              order: '03'
            }
          }
        }, {
          public_id: 'my_project/image_a',
          version: 1509653787,
          format: 'jpg',
          width: 2100,
          height: 1400,
          type: 'upload',
          created_at: '2017-11-02T14:06:52Z',
          context: {
            custom: {
              caption: 'Image A',
              order: '01'
            }
          }
        }],
        updated_at: '2017-11-03T13:16:29Z'
      }
      return [200, {
        'Content-Type': 'application/json'
      }, JSON.stringify(cloudinaryResourceListResponse)]
    });

    await render(hbs`
      {{#cloudinary-resource-list 'test' as |resourceList|}}
        {{#each resourceList.items as |item|}}
          <span>{{item.public_id}}</span>
        {{/each}}
      {{/cloudinary-resource-list}}
    `);

    assert.ok(find('span').textContent.trim().endsWith('image_a'), 'Image A order is OK');
    assert.ok(find('span:nth-child(2)').textContent.trim().endsWith('image_b'), 'Image B order is OK');
    assert.ok(find('span:nth-child(3)').textContent.trim().endsWith('image_c'), 'Image C order is OK');
  });

  test('it fetches images without custom context', async function(assert) {
    this.server.get('https://res.cloudinary.com/cloudinary-test/image/list/test.json', () => {
      let cloudinaryResourceListResponse = {
        resources: [
          {
            public_id: 'image_a',
            version: 1476216440,
{'fraction_non_alphanumeric': 0.14480570975416338, 'fraction_numerical': 0.0415543219666931, 'mean_word_length': 2.1404382470119523, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1790858', 'n_tokens_mistral': 2081, 'n_tokens_neox': 1895, 'n_words': 384}