"use strict";

var _react = _interopRequireDefault(require("react"));

var _enzyme = require("enzyme");

var _index = require("../index");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// manually trigger src load error. jsdom will not fire a load event
// https://github.com/jsdom/jsdom/issues/1816#issuecomment-310106280
function triggerLoadError(wrapper) {
  wrapper.instance().handleError();
  wrapper.update();
}

describe('Avatar', function () {
  it('applies expected accessibility attributes to img element', function () {
    var name = 'user name';
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Avatar, {
      name: name,
      src: "valid-img-src.png"
    }));
    var image = wrapper.find('img');
    expect(image.getDOMNode().getAttribute('alt')).toBe(name);
  });
  it('does not render img element if image fails to load', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Avatar, {
      name: "user name",
      src: "invalid-img-src.png"
    }));
    expect(wrapper.find('img')).toExist();
    triggerLoadError(wrapper);
    expect(wrapper.find('img')).not.toExist();
  });
  it('applies expected accessibility attributes to root by default', function () {
    var name = 'user name';
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Avatar, {
      name: name,
      src: "invalid-img-src.png"
    }));
    var rootNode = wrapper.childAt(0).getDOMNode();
    expect(rootNode.getAttribute('aria-label')).toBeNull();
    expect(rootNode.getAttribute('role')).toBeNull();
  });
  it('applies expected accessibility attributes to root if image fails to load', function () {
    var name = 'user name';
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.Avatar, {
      name: name,
      src: "invalid-img-src.png"
    }));
    var rootNode = wrapper.childAt(0).getDOMNode();
    triggerLoadError(wrapper);
    expect(rootNode.getAttribute('aria-label')).toBe(name);
    expect(rootNode.getAttribute('role')).toBe('img');
  });
});