test('it renders a check if isActive', function(assert) {
  let organization = { name: 'Aptible', id: 'o1' };
  this.setProperties({ organization, currentOrganization: organization });
  this.render(hbs('{{organization-switcher-option organization=organization currentOrganization=organization}}'));

  assert.equal(this.$('.fa-check.success').length, 1, 'element has a check mark');
});
test('Array document: has a radio elements', function(assert) {
  let newItem = this.arrayDocument.addItem();

  this.setProperties({ key: this.key, property: this.arrayProperty, newItem });
  this.render(hbs('{{schema-field-radio key=key property=property document=newItem}}'));

  assert.equal(this.$('input[type="radio"][name="developer"]').length, 2, 'has two radio buttons');
});
test('Object document nested property: has a select element with options', function(assert) {
  this.setProperties({ key: this.nestedKey, property: this.nestedProperty,
                       document: this.objectDocument });
  this.render(hbs('{{schema-field-select key=key property=property document=document}}'));

  assert.equal(this.$('select[name="address.state"]').length, 1, 'has a select element');
  assert.equal(this.$('select option').length, 6, 'has all 6 options');
});
test('Array document: has a select element with options', function(assert) {
  let newItem = this.arrayDocument.addItem();

  this.setProperties({ key: this.key, property: this.arrayProperty, newItem });
  this.render(hbs('{{schema-field-select key=key property=property document=newItem}}'));

  assert.equal(this.$('select[name="state"]').length, 1, 'has a select element');
  assert.equal(this.$('select option').length, 6, 'has all 6 options');
});
test('Object document nested property: uses default value if present', function(assert) {
  let defaultValue = 'NY';

  this.setProperties({ key: this.nestedKey, property: this.nestedProperty,
                       document: this.objectDocument });
  this.render(hbs('{{schema-field-select key=key property=property document=document}}'));

  let select = this.$('select');
  assert.equal(select.val(), defaultValue);
});
test('Array document: uses default value if present', function(assert) {
  let newItem = this.arrayDocument.addItem();
  let defaultValue = 'NY';

  this.setProperties({ key: this.key, property: this.arrayProperty, newItem });
  this.render(hbs('{{schema-field-select key=key property=property document=newItem}}'));

  let select = this.$('select');
  assert.equal(select.val(), defaultValue);
});
test('Array document: uses default value if present', function(assert) {
  let newItem = this.arrayDocument.addItem();
  let defaultValue = false;

  this.setProperties({ key: this.key, property: this.arrayProperty, newItem });
  this.render(hbs('{{schema-field-radio key=key property=property document=newItem}}'));

  let selectedRadio = this.$('input[type="radio"]:checked');
  assert.equal(JSON.parse(selectedRadio.val().toLowerCase()), defaultValue);
});
test('Array document: uses existing document value if present', function(assert) {
  let newItem = this.arrayDocument.addItem();
  newItem.set('developer', true);

  this.setProperties({ key: this.key, property: this.arrayProperty, newItem });
  this.render(hbs('{{schema-field-radio key=key property=property document=newItem}}'));

  let selectedRadio = this.$('input[type="radio"]:checked');

  assert.equal(JSON.parse(selectedRadio.val().toLowerCase()), newItem.get(this.key), 'documents current value used as default');
});
test('When a display prompt and default value are missing, use first valid value as default value', function(assert) {
  let schema = new Schema(selectWithoutPrompt);
  let document = schema.buildDocument();
  let property = schema.properties.state;
  let [expected] = noDefaultStateProperty.enum;

  this.setProperties({ key: 'state', property, document });
  this.render(hbs('{{schema-field-select key=key property=property document=document}}'));

  assert.equal(document.get('state'), expected, 'Initial value is first element in enum array');
});
test('Object document nested property: uses existing document value if present', function(assert) {
  this.objectDocument.set(this.nestedKey, 'RI');

  this.setProperties({ key: this.nestedKey, property: this.nestedProperty,
                       document: this.objectDocument });
  this.render(hbs('{{schema-field-select key=key property=property document=document}}'));

  let select = this.$('select');

  assert.equal(select.val(), this.objectDocument.get(this.nestedKey), 'documents current value used as default');
});
test('Array document: uses existing document value if present', function(assert) {
  let newItem = this.arrayDocument.addItem();
  newItem.set('state', 'NY');

  this.setProperties({ key: this.key, property: this.arrayProperty, newItem });
  this.render(hbs('{{schema-field-select key=key property=property document=newItem}}'));

  let select = this.$('select');

  assert.equal(select.val(), newItem.get(this.key), 'documents current value used as default');
});
test('a single string parameter passed to `hbs` works', function(assert) {
  var view = Ember.Component.create({
    greeting: "hello",
    layout: hbs('<h1>{{greeting}}</h1>')
  });

  Ember.run(function() {
    view.appendTo('#ember-testing');
  });

  assert.equal(view.$().html().trim(), "<h1>hello</h1>", "single string parameter works");
});
test('When `readonly` is true, select should be disabled', function(assert) {
  let schema = new Schema(disabledPropertySchema);
  let document = schema.buildDocument();
  let property = schema.properties.state;

  this.setProperties({ key: 'state', property, document });
  this.render(hbs('{{schema-field-select key=key property=property document=document}}'));

  let select = this.$('select');
  assert.ok(select.is(':disabled'), 'select is disabled');
  assert.equal(select.val(), 'IN', 'default value is used in select');
  assert.equal(document.get('state'), 'IN', 'document value is correct');
});
test('basic attributes are set', function(assert) {
  let stack = Ember.Object.create({ handle: 'stack1', apps: [], containerUsage: 10 });
  this.set('stacks', [stack]);
  this.render(hbs('{{usage-quote-by-resource stacks=stacks allowance=6 hourlyRate=8 plan="platform" resource="container"}}'));

  let title = this.$('.resource-label');
  let rate = this.$('.resource-rate');
  let stackHandle = this.$('.stack-handle');

  assert.equal(title.text(), 'Containers', 'has a resource title');
  assert.equal(rate.text(), '$0.08 per hour', 'has a resource rate');
  assert.equal($.trim(stackHandle.text()), 'stack1', 'has a stack');
});
test('disk usage exceeding allowance results in overage billing', function(assert) {
  let stack1 = Ember.Object.create({ handle: 'stack1', databases: [], totalDiskSize: 800 });
  let stack2 = Ember.Object.create({ handle: 'stack2', databases: [], totalDiskSize: 900 });
  this.set('stacks', [stack1, stack2]);
  this.render(hbs('{{usage-quote-by-resource stacks=stacks allowance=1000 hourlyRate=0.0507 plan="platform" resource="disk"}}'));

  let total = this.$('.resource-usage-total .usage-value');
  let allowance = this.$('.allowance');
  let net = this.$('.net-usage');
  assert.equal($.trim(total.text()), '$259.08', 'has overage billing total');
  assert.equal(allowance.text(), '-1000', 'has an allowance');
  assert.equal(net.text(), '700', 'has 700 net usage');
});
test('Array document: updates document when changed', function(assert) {
  let newItem = this.arrayDocument.addItem();
  let expected = true;

  this.setProperties({ key: this.key, property: this.arrayProperty, newItem });
  this.render(hbs('{{schema-field-radio key=key property=property document=newItem}}'));

  let selectedRadio = this.$('input[type="radio"]:checked');
  assert.equal(JSON.parse(selectedRadio.val().toLowerCase()), false, 'initial value is correct');

  this.$('input[type="radio"][value="true"]').click();

  assert.equal(newItem.get(this.key), expected);
});
test('disk usage less than allowance results in 0 billing', function(assert) {
  let stack1 = Ember.Object.create({ handle: 'stack1', databases: [], totalDiskSize: 200 });
  let stack2 = Ember.Object.create({ handle: 'stack2', databases: [], totalDiskSize: 300 });
  this.set('stacks', [stack1, stack2]);
  this.render(hbs('{{usage-quote-by-resource stacks=stacks allowance=1000 hourlyRate=0.0507 plan="platform" resource="disk"}}'));

  let total = this.$('.resource-usage-total .usage-value');
  let allowance = this.$('.allowance');
  let net = this.$('.net-usage');
  let resourceRate = this.$('.resource-rate');

  assert.equal($.trim(total.text()), '$0.00', 'has 0 billing total');
  assert.equal(allowance.text(), '-1000', 'has an allowance');
  assert.equal(net.text(), '0', 'has 0 net usage');
  assert.equal($.trim(resourceRate.text()), '$0.37/GB per month', 'has a resource rate');
});
test('Array document: updates document when changed', function(assert) {
  let newItem = this.arrayDocument.addItem();
  let expected = 'IN';

  this.setProperties({ key: this.key, property: this.arrayProperty, newItem });
  this.render(hbs('{{schema-field-select key=key property=property document=newItem}}'));

  let select = this.$('select');

  assert.equal(select.val(), 'NY', 'initial value is correct');

  select.val(expected);
  select.trigger('change');

  assert.equal(newItem.get(this.key), expected);
});
test('Object document nested property: updates document when changed', function(assert) {
  let expected = 'IN';

  this.setProperties({ key: this.nestedKey, property: this.nestedProperty,
                       document: this.objectDocument });
  this.render(hbs('{{schema-field-select key=key property=property document=document}}'));

  let select = this.$('select');

  assert.equal(select.val(), 'NY', 'initial value is correct');

  select.val(expected);
  select.trigger('change');

  assert.equal(this.objectDocument.get(this.nestedKey), expected);
});
test('container usage exceeding allowance results in overage billing', function(assert) {
  let stack1 = Ember.Object.create({ handle: 'stack1', apps: [], containerUsage: 8 });
  let stack2 = Ember.Object.create({ handle: 'stack2', apps: [], containerUsage: 9 });
  let service1 = Ember.Object.create({ usage: 8 });
  let service2 = Ember.Object.create({ usage: 9 });
  this.set('stacks', [stack1, stack2]);
  this.set('services', [service1, service2]);
  this.render(hbs('{{usage-quote-by-resource stacks=stacks services=services allowance=6 hourlyRate=8 plan="platform" resource="container"}}'));

  let total = this.$('.resource-usage-total .usage-value');
  let allowance = this.$('.allowance');
  let net = this.$('.net-usage');

  assert.equal($.trim(total.text()), '$642.40', 'has overage billing total');
  assert.equal(allowance.text(), '-6', 'has an allowance');
  assert.equal(net.text(), '11', 'has 11 net usage');
});
test('When `readonly` is true, radios should be disabled', function(assert) {
  let schema = new Schema(disabledPropertySchema);
  let document = schema.buildDocument();
  let property = schema.properties.developer;

  this.setProperties({ key: 'developer', property, document });
  this.render(hbs('{{schema-field-radio key=key property=property document=document}}'));

  let trueInput = this.$('input[type="radio"][value="true"]');
  let falseInput = this.$('input[type="radio"][value="false"]');

  assert.ok(trueInput.is(':disabled'), 'true input is disabled');
  assert.ok(falseInput.is(':disabled'), 'false input is disabled');
  assert.ok(trueInput.is(':checked'), 'true input is checked by default');
  assert.ok(!falseInput.is(':checked'), 'false input is not checked by default');
  assert.equal(document.get('developer'), true, 'document value is correct');
});
test('container usage less than allowance results in 0 billing', function(assert) {
  let stack1 = Ember.Object.create({ handle: 'stack1', apps: [], containerUsage: 2 });
  let stack2 = Ember.Object.create({ handle: 'stack2', apps: [], containerUsage: 3 });
  let service1 = Ember.Object.create({ usage: 2 });
  let service2 = Ember.Object.create({ usage: 3 });
  this.set('stacks', [stack1, stack2]);
  this.set('services', [service1, service2]);
  this.render(hbs('{{usage-quote-by-resource stacks=stacks services=services allowance=6 hourlyRate=8 plan="platform" resource="container"}}'));

  let total = this.$('.resource-usage-total .usage-value');
  let allowance = this.$('.allowance');
  let net = this.$('.net-usage');
  let resourceRate = this.$('.resource-rate');

  assert.equal($.trim(total.text()), '$0.00', 'has 0 billing total');
  assert.equal(allowance.text(), '-6', 'has an allowance');
  assert.equal(net.text(), '0', 'has 0 net usage');
  assert.equal($.trim(resourceRate.text()), '$0.08 per hour', 'has a resource rate');
});
import Component from '@ember/component';
import hbs from 'htmlbars-inline-precompile';

export default Component.extend({
  greeting: "hello",
  layout: hbs('<h1>{{greeting}}</h1>')
});