<commit_before>/* Copyright (C) 2017 Canonical Ltd. */
'use strict';

const React = require('react');

const UnitList = require('./unit-list');
const ButtonRow = require('../../button-row/button-row');
const CheckListItem = require('../../check-list-item/check-list-item');
const OverviewAction = require('../overview-action/overview-action');

const jsTestUtils = require('../../../utils/component-test-utils');
const testUtils = require('react-dom/test-utils');

describe('UnitList', () => {
  var acl, service;

  beforeEach(() => {
    acl = {isReadOnly: sinon.stub().returns(false)};
    service = {
      get: function(val) {
        if (val === 'subordinate') {
          return false;
        }
        return 'mysql';
      }
    };
  });

  it('renders if there are no units', () => {
    var renderer = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={service}
        units={[]}
        whenChanged={sinon.stub()} />, true);
    var instance = renderer.getMountedInstance();
    var output = renderer.getRenderOutput();
    var expected = (
      <div className="unit-list">
        <div className="unit-list__actions">
          <OverviewAction
            action={instance._navigate}
            icon="plus_box_16"
            title="Scale application" />
        </div>
        <div className="unit-list__message">
          No units for this application. Scale to add units.
        </div>
        {undefined}
      </div>);
    expect(output).toEqualJSX(expected);
  });

  it('renders a list of unit components', () => {
    var units = [{
      displayName: 'mysql/0',
      id: 'mysql/0'
    }, {
      displayName: 'mysql/1',
      id: 'mysql/1'
    }];
    var renderer = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={service}
        units={units}
        whenChanged={sinon.stub()} />, true);
    var instance = renderer.getMountedInstance();
    var output = renderer.getRenderOutput();
    var children = output.props.children[1].props.children;
    var refs = [
      'CheckListItem-' + units[0].id,
      'CheckListItem-' + units[1].id
    ];
    const expected = (
      <ul className="unit-list__units">
        {[<CheckListItem
          disabled={false}
          ref="select-all"
          key="select-all"
          className="select-all"
          label="Select all units"
          aside="2"
          whenChanged={children[0].props.whenChanged} />,
        <CheckListItem
          disabled={false}
          key={units[0].displayName}
          ref={refs[0]}
          label={units[0].displayName}
          action={children[1].props.action}
          extraInfo={undefined}
          id="mysql/0"
          whenChanged={instance._updateActiveCount} />,
        <CheckListItem
          disabled={false}
          key={units[1].displayName}
          ref={refs[1]}
          label={units[1].displayName}
          action={children[2].props.action}
          extraInfo={undefined}
          id="mysql/1"
          whenChanged={instance._updateActiveCount} />]}
      </ul>);
    expect(output.props.children[1]).toEqualJSX(expected);
  });

  it('renders a grouped list of error unit components', () => {
    var units = [{
      displayName: 'mysql/0',
      id: 'mysql/0',
      agent_state_info: 'hook failed: install'
    }, {
      displayName: 'mysql/1',
      id: 'mysql/1',
      agent_state_info: 'hook failed: config-changed'
    }];
    var renderer = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={{}}
        unitStatus='error'
        units={units}
        whenChanged={sinon.stub()} />, true);
    var instance = renderer.getMountedInstance();
    var output = renderer.getRenderOutput();
    var children = output.props.children[1].props.children;
    var refs = [
      'CheckListItem-' + units[0].id,
      'CheckListItem-' + units[1].id
    ];
    const expected = (
      <ul className="unit-list__units">
        {[<CheckListItem
          aside="1"
          disabled={false}
          ref="select-all-0"
          key="select-all-0"
          label="hook failed: install"
          className="select-all"
          whenChanged={children[0].props.whenChanged} />,
        <CheckListItem
          disabled={false}
          key={units[0].displayName}
          ref={refs[0]}
          label={units[0].displayName}
          action={children[1].props.action}
          extraInfo={undefined}
          id="mysql/0"
          whenChanged={instance._updateActiveCount} />,
        <CheckListItem
          aside="1"
          disabled={false}
          ref="select-all-1"
          key="select-all-1"
          label="hook failed: config-changed"
          className="select-all"
          whenChanged={children[2].props.whenChanged} />,
        <CheckListItem
          disabled={false}
          key={units[1].displayName}
          ref={refs[1]}
          label={units[1].displayName}
          action={children[3].props.action}
          extraInfo={undefined}
          id="mysql/1"
          whenChanged={instance._updateActiveCount} />]}
      </ul>);
    expect(output.props.children[1]).toEqualJSX(expected);
  });

  it('displays the provided count', function() {
    const renderer = jsTestUtils.shallowRender(
      <CheckListItem
        aside="5"
        label="label"
        whenChanged={sinon.stub()} />, true);
    const output = renderer.getRenderOutput();
    assert.equal(output.props.children.props.children[3].props.children, '5');
  });

  it('renders the Scale Application action component', () => {
    var units = [{
      displayName: 'mysql/0'
    }];

    var output = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={service}
        units={units}
        whenChanged={sinon.stub()} />);
    var child = output.props.children[0].props.children;
    const expected = (
      <OverviewAction<commit_msg>Add a test for not showing the scaling link when read only.<commit_after>/* Copyright (C) 2017 Canonical Ltd. */
'use strict';

const React = require('react');

const UnitList = require('./unit-list');
const ButtonRow = require('../../button-row/button-row');
const CheckListItem = require('../../check-list-item/check-list-item');
const OverviewAction = require('../overview-action/overview-action');

const jsTestUtils = require('../../../utils/component-test-utils');
const testUtils = require('react-dom/test-utils');

describe('UnitList', () => {
  var acl, service;

  beforeEach(() => {
    acl = {isReadOnly: sinon.stub().returns(false)};
    service = {
      get: function(val) {
        if (val === 'subordinate') {
          return false;
        }
        return 'mysql';
      }
    };
  });

  it('renders if there are no units', () => {
    var renderer = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={service}
        units={[]}
        whenChanged={sinon.stub()} />, true);
    var instance = renderer.getMountedInstance();
    var output = renderer.getRenderOutput();
    var expected = (
      <div className="unit-list">
        <div className="unit-list__actions">
          <OverviewAction
            action={instance._navigate}
            icon="plus_box_16"
            title="Scale application" />
        </div>
        <div className="unit-list__message">
          No units for this application. Scale to add units.
        </div>
        {undefined}
      </div>);
    expect(output).toEqualJSX(expected);
  });

  it('renders a list of unit components', () => {
    var units = [{
      displayName: 'mysql/0',
      id: 'mysql/0'
    }, {
      displayName: 'mysql/1',
      id: 'mysql/1'
    }];
    var renderer = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={service}
        units={units}
        whenChanged={sinon.stub()} />, true);
    var instance = renderer.getMountedInstance();
    var output = renderer.getRenderOutput();
    var children = output.props.children[1].props.children;
    var refs = [
      'CheckListItem-' + units[0].id,
      'CheckListItem-' + units[1].id
    ];
    const expected = (
      <ul className="unit-list__units">
        {[<CheckListItem
          disabled={false}
          ref="select-all"
          key="select-all"
          className="select-all"
          label="Select all units"
          aside="2"
          whenChanged={children[0].props.whenChanged} />,
        <CheckListItem
          disabled={false}
          key={units[0].displayName}
          ref={refs[0]}
          label={units[0].displayName}
          action={children[1].props.action}
          extraInfo={undefined}
          id="mysql/0"
          whenChanged={instance._updateActiveCount} />,
        <CheckListItem
          disabled={false}
          key={units[1].displayName}
          ref={refs[1]}
          label={units[1].displayName}
          action={children[2].props.action}
          extraInfo={undefined}
          id="mysql/1"
          whenChanged={instance._updateActiveCount} />]}
      </ul>);
    expect(output.props.children[1]).toEqualJSX(expected);
  });

  it('renders a grouped list of error unit components', () => {
    var units = [{
      displayName: 'mysql/0',
      id: 'mysql/0',
      agent_state_info: 'hook failed: install'
    }, {
      displayName: 'mysql/1',
      id: 'mysql/1',
      agent_state_info: 'hook failed: config-changed'
    }];
    var renderer = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={{}}
        unitStatus='error'
        units={units}
        whenChanged={sinon.stub()} />, true);
    var instance = renderer.getMountedInstance();
    var output = renderer.getRenderOutput();
    var children = output.props.children[1].props.children;
    var refs = [
      'CheckListItem-' + units[0].id,
      'CheckListItem-' + units[1].id
    ];
    const expected = (
      <ul className="unit-list__units">
        {[<CheckListItem
          aside="1"
          disabled={false}
          ref="select-all-0"
          key="select-all-0"
          label="hook failed: install"
          className="select-all"
          whenChanged={children[0].props.whenChanged} />,
        <CheckListItem
          disabled={false}
          key={units[0].displayName}
          ref={refs[0]}
          label={units[0].displayName}
          action={children[1].props.action}
          extraInfo={undefined}
          id="mysql/0"
          whenChanged={instance._updateActiveCount} />,
        <CheckListItem
          aside="1"
          disabled={false}
          ref="select-all-1"
          key="select-all-1"
          label="hook failed: config-changed"
          className="select-all"
          whenChanged={children[2].props.whenChanged} />,
        <CheckListItem
          disabled={false}
          key={units[1].displayName}
          ref={refs[1]}
          label={units[1].displayName}
          action={children[3].props.action}
          extraInfo={undefined}
          id="mysql/1"
          whenChanged={instance._updateActiveCount} />]}
      </ul>);
    expect(output.props.children[1]).toEqualJSX(expected);
  });

  it('displays the provided count', function() {
    const renderer = jsTestUtils.shallowRender(
      <CheckListItem
        aside="5"
        label="label"
        whenChanged={sinon.stub()} />, true);
    const output = renderer.getRenderOutput();
    assert.equal(output.props.children.props.children[3].props.children, '5');
  });

  it('does not show the scaling link when read only', () => {
    acl.isReadOnly.returns(true);
    const units = [{
      displayName: 'mysql/0'
    }];
    const output = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={service}
        units={units}
        whenChanged={sinon.stub()} />);
    const scaling = output.props.children[0];
    assert.equal(scaling, null);
  });

  it('renders the Scale Application action component', () => {
    var units = [{
      displayName: 'mysql/0'
    }];

    var output = jsTestUtils.shallowRender(
      <UnitList
        acl={acl}
        changeState={sinon.stub()}
        destroyUnits={sinon.stub()}
        envResolved={sinon.stub()}
        service={service}
        units={units}
        whenChanged={sinon.stub()} />);
    var child = output.props.children[0].props.children;
    const expected = (
      <OverviewAction
{'fraction_non_alphanumeric': 0.15264750378214825, 'fraction_numerical': 0.008774583963691377, 'mean_word_length': 2.2692878338278932, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 63, 'https://': 0, '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': '6746055', 'n_tokens_mistral': 3903, 'n_tokens_neox': 3924, 'n_words': 676}