<commit_before>import test from 'ava';
import nock from 'nock';
import sinon from 'sinon';
import Avatar from '../build/avatar';

let avatar;
let image;
const gravatar_timeout = 1600;

test.before(() => {
  nock.disableNetConnect();
  nock.cleanAll();

  // Useful when adding new tests that make calls.
  // nock.recorder.rec({
  //   dont_print: true,
  // });

  const request = nock('https://secure.gravatar.com/')
    .filteringRequestBody(() => '*')
    .defaultReplyHeaders({
      'Content-Type': 'content-type:image/jpeg',
    })
    .get('/avatar/12929016fffb0b3af98bc440acf0bfe2?d=404')
    .reply(200, 'JPEG')
    .get('/avatar/00000000000000000000000000000000?d=404')
    .reply(404, '');

  request.on('error', (err) => {
    console.log('NOCK ERROR:', err); // eslint-disable-line no-console
  });
});

test.beforeEach(() => {
  document.write('');
  document.write('<div id=\'avatar-1\'></div>');
  image = document.getElementById('avatar-1');
});

test.afterEach(() => {
  image = null;
  avatar = null;
});

test('#constructor should throw an error without an element', (t) => {
  t.throws(Avatar, 'Cannot call a class as a function');
});

test('#constructor should throw an error if there is no element provided', (t) => {
  const fn = () => {
    avatar = new Avatar();
  };
  const error = t.throws(() => { fn(); }, Error);
  t.is(error.message, 'No image element provided.');
});

test('#constructor should render', (t) => {
  avatar = new Avatar(image);
  t.true(avatar.settings.useGravatar);
});

test('#constructor should allow options', (t) => {
  avatar = new Avatar(image, {
    useGravatar: false,
  });
  t.false(avatar.settings.useGravatar);
});

test('#constructor should allow Gravatar fallbacks', (t) => {
  avatar = new Avatar(image, {
    useGravatar: true,
    allowGravatarFallback: true,
  });
  t.true(avatar.settings.useGravatar);
  t.true(avatar.settings.allowGravatarFallback);
});

test('#constructor should render a canvas', (t) => {
  avatar = new Avatar(image, {
    useGravatar: false,
    initials: 'MC',
  });
  t.false(avatar.settings.useGravatar);
  t.not(avatar.settings.useGravatar, 'MC');
});

test('#constructor should be able to set the settings', (t) => {
  const options = {
    useGravatar: false,
    allowGravatarFallback: true,
    initials: 'MDC',
    initial_fg: '#111',
    initial_bg: '#222',
    initial_size: 1,
    initial_weight: 2,
    initial_font_family: 'Comic Sans',
    hash: '00000000000000000000000000000000',
    email: '<EMAIL>',
    size: 120,
    fallback: 'mm',
    rating: 'pg',
    setSourceCallback: null,
    forcedefault: true,
    fallbackImage: 'nah',
    debug: false,
    github_id: 1,
    use_avatars_io: true,
    avatars_io: {
      user_id: 1,
      identifier: 2,
      twitter: 3,
      facebook: 4,
      instagram: 5,
    },
    extra: () => true,
  };
  avatar = new Avatar(image, options);
  t.deepEqual(avatar.settings, options);
});

test('#setSource should throw an error if there is no element', (t) => {
  const fn = () => {
    avatar = new Avatar(image);
    delete avatar.element;
    avatar.setSource();
  };
  t.throws(fn, Error);
  t.throws(fn, 'No image element set.');
});

test('#setSource should set the src attribute', (t) => {
  avatar = new Avatar(image);
  avatar.setSource('data:image/png;');
  t.is(image.src, 'data:image/png;');

  avatar.setSource('http://placekitten.com/200/300');
  t.is(image.src, 'http://placekitten.com/200/300');

  avatar.setSource();
  t.is(image.src, 'http://placekitten.com/200/300');
});

test('#setSource should do nothing if there is no source provided', (t) => {
  avatar = new Avatar(image);
  avatar.setSource('data:image/png;');
  t.is(image.src, 'data:image/png;');

  avatar.setSource();
  t.is(image.src, 'data:image/png;');
});

test('#initialAvatar should return a PNG from element size', (t) => {
  avatar = new Avatar(image);
  const png = avatar.initialAvatar('MC');<commit_msg>Added set source callback test<commit_after>import test from 'ava';
import nock from 'nock';
import sinon from 'sinon';
import Avatar from '../build/avatar';

let avatar;
let image;
const gravatar_timeout = 1600;

test.before(() => {
  nock.disableNetConnect();
  nock.cleanAll();

  // Useful when adding new tests that make calls.
  // nock.recorder.rec({
  //   dont_print: true,
  // });

  const request = nock('https://secure.gravatar.com/')
    .filteringRequestBody(() => '*')
    .defaultReplyHeaders({
      'Content-Type': 'content-type:image/jpeg',
    })
    .get('/avatar/12929016fffb0b3af98bc440acf0bfe2?d=404')
    .reply(200, 'JPEG')
    .get('/avatar/00000000000000000000000000000000?d=404')
    .reply(404, '');

  request.on('error', (err) => {
    console.log('NOCK ERROR:', err); // eslint-disable-line no-console
  });
});

test.beforeEach(() => {
  document.write('');
  document.write('<div id=\'avatar-1\'></div>');
  image = document.getElementById('avatar-1');
});

test.afterEach(() => {
  image = null;
  avatar = null;
});

test('#constructor should throw an error without an element', (t) => {
  t.throws(Avatar, 'Cannot call a class as a function');
});

test('#constructor should throw an error if there is no element provided', (t) => {
  const fn = () => {
    avatar = new Avatar();
  };
  const error = t.throws(() => { fn(); }, Error);
  t.is(error.message, 'No image element provided.');
});

test('#constructor should render', (t) => {
  avatar = new Avatar(image);
  t.true(avatar.settings.useGravatar);
});

test('#constructor should allow options', (t) => {
  avatar = new Avatar(image, {
    useGravatar: false,
  });
  t.false(avatar.settings.useGravatar);
});

test('#constructor should allow Gravatar fallbacks', (t) => {
  avatar = new Avatar(image, {
    useGravatar: true,
    allowGravatarFallback: true,
  });
  t.true(avatar.settings.useGravatar);
  t.true(avatar.settings.allowGravatarFallback);
});

test('#constructor should render a canvas', (t) => {
  avatar = new Avatar(image, {
    useGravatar: false,
    initials: 'MC',
  });
  t.false(avatar.settings.useGravatar);
  t.not(avatar.settings.useGravatar, 'MC');
});

test('#constructor should be able to set the settings', (t) => {
  const options = {
    useGravatar: false,
    allowGravatarFallback: true,
    initials: 'MDC',
    initial_fg: '#111',
    initial_bg: '#222',
    initial_size: 1,
    initial_weight: 2,
    initial_font_family: 'Comic Sans',
    hash: '00000000000000000000000000000000',
    email: '<EMAIL>',
    size: 120,
    fallback: 'mm',
    rating: 'pg',
    forcedefault: true,
    fallbackImage: 'nah',
    debug: false,
    github_id: 1,
    use_avatars_io: true,
    avatars_io: {
      user_id: 1,
      identifier: 2,
      twitter: 3,
      facebook: 4,
      instagram: 5,
    },
    setSourceCallback: null,
    extra: () => true,
  };
  avatar = new Avatar(image, options);
  t.deepEqual(avatar.settings, options);
});

test('#setSource should throw an error if there is no element', (t) => {
  const fn = () => {
    avatar = new Avatar(image);
    delete avatar.element;
    avatar.setSource();
  };
  t.throws(fn, Error);
  t.throws(fn, 'No image element set.');
});

test('#setSource should set the src attribute', (t) => {
  avatar = new Avatar(image);
  avatar.setSource('data:image/png;');
  t.is(image.src, 'data:image/png;');

  avatar.setSource('http://placekitten.com/200/300');
  t.is(image.src, 'http://placekitten.com/200/300');

  avatar.setSource();
  t.is(image.src, 'http://placekitten.com/200/300');
});

test.cb('#setSource should call setSourceCallback if provided', (t) => {
  var _source = null;

  avatar = new Avatar(image, {
    setSourceCallback: function(source) {
      _source = source;
    }
  });
  avatar.setSource('data:image/png;');

  setTimeout(() => {
    t.is(_source, 'data:image/png;');
    t.end();
  }, gravatar_timeout);
});

test('#setSource should do nothing if there is no source provided', (t) => {
  avatar = new Avatar(image);
  avatar.setSource('data:image/png;');
  t.is(image.src, 'data:image/png;');

  avatar.setSource();
  t.is(image.src, 'data:image/png;');
});

test('#initialAvatar should return a PNG from element size', (t) => {
  avatar = new Avatar(image);
  const png = avatar.initialAvatar('MC');
{'fraction_non_alphanumeric': 0.17960518348068305, 'fraction_numerical': 0.03245730894998183, 'mean_word_length': 3.6393258426966293, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 51, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '616023', 'n_tokens_mistral': 3123, 'n_tokens_neox': 2847, 'n_words': 648}