/*
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.
*/
import React from 'react';
import { mount } from 'enzyme';
import { Avatar, StyledInitials } from '../index.js'; // 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 = mount(React.createElement(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 = mount(React.createElement(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 = mount(React.createElement(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 = mount(React.createElement(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');
  });
  it('renders user first 2 initials when image fails to load', function () {
    var name = 'user name';
    var wrapper = mount(React.createElement(Avatar, {
      name: name,
      src: "invalid-img-src.png"
    }));
    triggerLoadError(wrapper);
    expect(wrapper.find(StyledInitials).text()).toBe('UN');
  });
  it('only renders 2 initials if more names exist when image fails to load', function () {
    var name = 'user name surname';
    var wrapper = mount(React.createElement(Avatar, {
      name: name,
      src: "invalid-img-src.png"
    }));
    triggerLoadError(wrapper);
    expect(wrapper.find(StyledInitials).text()).toBe('UN');
  });
  it('only renders 1 initial if one name exists when image fails to load', function () {
    var name = 'user';
    var wrapper = mount(React.createElement(Avatar, {
      name: name,
      src: "invalid-img-src.png"
    }));
    triggerLoadError(wrapper);
    expect(wrapper.find(StyledInitials).text()).toBe('U');
  });
});