<commit_before>import { setupRenderingTest } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { render, getContext } from '@ember/test-helpers';
import { htmlSafe } from '@ember/string';
import { pluralize } from 'ember-inflector';

/**
 * This should be used as a replacement for `setupRenderingTest()`
 * and additionally sets up a few other things that help when
 * testing cards.
 */
export function setupCardTest(hooks) {
  setupRenderingTest(hooks);
  setupURLs(hooks);
}

// This is a workaround for https://github.com/intercom/ember-href-to/issues/94
// We use href-to internally in cardstack-url. So if you want to generate a link
// in a rendering test, you need use this.
export function setupURLs(hooks) {
  hooks.beforeEach(function() {
    this.owner.lookup('router:main').setupRouter()
  })
}

export function findCard(type, id, format='isolated') {
  return getContext().owner.lookup('service:cardstackData').load(type, id, format);
}

export function getSpaceForCard(type, id) {
  return getContext().owner.lookup('service:store').findRecord('space', `/${pluralize(type)}/${id}`);
}

export function renderCard(type, id, format, options = {}) {
  return getSpaceForCard(type, id).then(space => {
    let context = getContext();
    let card = space.get('primaryCard');
    let params = space.get('params');
    context.set('card', card);
    context.set('format', format);
    context.set('params', Object.assign({}, params, options.params || {}));

    if (options.width) {
      context.set('widthStyle', htmlSafe(`width: ${options.width}`));
      return render(hbs`
      <div style="{{widthStyle}}">
        {{cardstack-content event-isolated content=card format=format params=params }}
      </div>`);
    } else {
      return render(hbs`{{cardstack-content event-isolated content=card format=format params=params }}`);
    }
  });
}<commit_msg>test-support: Introduce `setupCardTestComponent()` helper<commit_after>import { setupRenderingTest } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

import Component from '@ember/component';
import { computed } from '@ember/object';
import { readOnly } from '@ember/object/computed';
import { htmlSafe } from '@ember/string';
import { render, getContext } from '@ember/test-helpers';

import { task } from 'ember-concurrency';
import { pluralize } from 'ember-inflector';

/**
 * This should be used as a replacement for `setupRenderingTest()`
 * and additionally sets up a few other things that help when
 * testing cards.
 */
export function setupCardTest(hooks) {
  setupRenderingTest(hooks);
  setupCardTestComponent(hooks);
  setupURLs(hooks);
}

// This is a workaround for https://github.com/intercom/ember-href-to/issues/94
// We use href-to internally in cardstack-url. So if you want to generate a link
// in a rendering test, you need use this.
export function setupURLs(hooks) {
  hooks.beforeEach(function() {
    this.owner.lookup('router:main').setupRouter()
  })
}

export function findCard(type, id, format='isolated') {
  return getContext().owner.lookup('service:cardstackData').load(type, id, format);
}

export function getSpaceForCard(type, id) {
  return getContext().owner.lookup('service:store').findRecord('space', `/${pluralize(type)}/${id}`);
}

export function renderCard(type, id, format, options = {}) {
  return getSpaceForCard(type, id).then(space => {
    let context = getContext();
    let card = space.get('primaryCard');
    let params = space.get('params');
    context.set('card', card);
    context.set('format', format);
    context.set('params', Object.assign({}, params, options.params || {}));

    if (options.width) {
      context.set('widthStyle', htmlSafe(`width: ${options.width}`));
      return render(hbs`
      <div style="{{widthStyle}}">
        {{cardstack-content event-isolated content=card format=format params=params }}
      </div>`);
    } else {
      return render(hbs`{{cardstack-content event-isolated content=card format=format params=params }}`);
    }
  });
}

/**
 * This sets up a `cardstack-card-test` component that can be use to render
 * cards in the QUnit test fixture:
 *
 * ```js
 * await render(hbs`{{cardstack-card-test "works-detail" 123 format="embedded"}}`);
 * ```
 *
 * The positional parameters are the card name and the ID. The component also
 * supports optional `format` and `params` parameters.
 */
export function setupCardTestComponent(hooks) {
  hooks.beforeEach(function() {
    let CardTestComponent = Component.extend({
      tagName: '',

      // inputs
      type: null,
      id: null,
      format: 'isolated',
      params: null,

      // filled by `getSpaceForCardTask`
      space: null,

      // derived data
      card: readOnly('space.primaryCard'),
      _params: computed('space', 'params', function() {
        Object.assign({}, this.get('space.params'), this.params)
      }),

      getSpaceForCardTask: task(function*() {
        let space = yield getSpaceForCard(this.type, this.id);
        this.set('space', space);
      }).on('didInsertElement').cancelOn('willDestroyElement'),
    });

    CardTestComponent.reopenClass({
      positionalParams: ['type', 'id'],
    });

    this.owner.register('component:cardstack-card-test', CardTestComponent);

    this.owner.register('template:components/cardstack-card-test',
      hbs`{{#if card}}{{cardstack-content event-isolated content=card format=format params=params}}{{/if}}`);
  });
}
{'fraction_non_alphanumeric': 0.15343818580833943, 'fraction_numerical': 0.001280175566934894, 'mean_word_length': 4.125585754451734, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '2888408', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1625, 'n_words': 409}