<commit_before>import { _ } from 'lodash';
import fakeFetch from 'fake-fetch';
import { Container } from 'aurelia-dependency-injection';
import { StageComponent } from 'aurelia-testing';
import { bootstrap } from 'aurelia-bootstrapper';

import { Config } from '../../src/config';
import { Collection } from '../../src/collection';

describe('Collection', () => {
  let component;
  let container;
  let config;

  beforeAll(done => {
    fakeFetch.install();
    component = StageComponent.withResources();
    component.create(bootstrap)
      .then(() => {
        container = Container.instance;
        config = container.get(Config);
        done();
      });
  });

  afterAll(() => {
    fakeFetch.restore();
    component.dispose();
  });

  describe('.configure()', () => {
    it('Should properly initialize the collection', () => {
      let collection = new Collection();
      collection.configure('myKey');

      expect(collection.container).toBe(container);
      expect(collection.modelid).toBe('_id');
      expect(collection.modelClass).toBeUndefined();
      expect(collection.defaultRoute).toBe('/api/myKey/');
      expect(collection.collection instanceof Array).toBe(true);
      expect(collection.refKeys()).toEqual([]);
      expect(collection.isComplete()).toBeTruthy();
    });
  });

  describe('.fromJSON', () => {
    let collection;

    beforeEach(() => {
      let creator = (data) => data;
      collection = new Collection();
      config.registerCollection('myKey', collection, 'default/route/', creator);
    });

    it('Should return null when data is undefined or null', () => {
      collection.fromJSON().then(res => {
        expect(res).toEqual(null);
      });
    });

    it('Should return a new model when data has an id not found in the collection', () => {
      collection.fromJSON({ _id: 'myId', foo: 21 }).then(res => {
        expect(res).toEqual({ _id: 'myId', foo: 21 });
      });
    });

    it('Should return a new model when data has an id not found in the collection', () => {
      collection.fromJSON({ _id: 'myId', foo: 21 }, { ignoreCollection: true })
        .then(res => {
          expect(res).toEqual({ _id: 'myId', foo: 21 });
          expect(collection.collection).toEqual([]);
        });
    });

    it('Should return a existing model, _syncFrom depending on isComplete, if data id is found in the collection', () => {
      let model = {
        _id: 'myId',
        foo: 42
      };
      collection.collection.push(model);

      collection.fromJSON({ _id: 'myId' }).then(res => {
        expect(res).toEqual(model);
      });
    });
  });

  describe('.toJSON()', () => {
    let model;
    let collection;

    beforeEach(() => {
      let creator = (data) => data;
      model = { _id: 'fakeId', other: 'field' };
      collection = new Collection();
      config.registerCollection('myKey', collection, 'default/route/', creator);
    });

    it('Should return the model when the model doesn\'t have a toJSON method', () => {
      let result = collection.toJSON(model);
      expect(result).toEqual(model);
    });

    it('Should call the toJSON method of a model', () => {
      model.toJSON = () => 'myFakeJSON';

      let result = collection.toJSON(model);
      expect(result).toBe('myFakeJSON');<commit_msg>chore(tests): Use of the `done` callback to prevent asynchronous results to give wrong results to the tests.<commit_after>import { _ } from 'lodash';
import fakeFetch from 'fake-fetch';
import { Container } from 'aurelia-dependency-injection';
import { StageComponent } from 'aurelia-testing';
import { bootstrap } from 'aurelia-bootstrapper';

import { Config } from '../../src/config';
import { Collection } from '../../src/collection';

describe('Collection', () => {
  let component;
  let container;
  let config;

  beforeAll(done => {
    fakeFetch.install();
    component = StageComponent.withResources();
    component.create(bootstrap)
      .then(() => {
        container = Container.instance;
        config = container.get(Config);
        done();
      });
  });

  afterAll(() => {
    fakeFetch.restore();
    component.dispose();
  });

  describe('.configure()', () => {
    it('Should properly initialize the collection', () => {
      let collection = new Collection();
      collection.configure('myKey');

      expect(collection.container).toBe(container);
      expect(collection.modelid).toBe('_id');
      expect(collection.modelClass).toBeUndefined();
      expect(collection.defaultRoute).toBe('/api/myKey/');
      expect(collection.collection instanceof Array).toBe(true);
      expect(collection.refKeys()).toEqual([]);
      expect(collection.isComplete()).toBeTruthy();
    });
  });

  describe('.fromJSON', () => {
    let collection;

    beforeEach(() => {
      let creator = (data) => data;
      collection = new Collection();
      config.registerCollection('myKey', collection, 'default/route/', creator);
    });

    it('Should return null when data is undefined or null', done => {
      collection.fromJSON().then(res => {
        expect(res).toEqual(null);
        done();
      });
    });

    it('Should return a new model when data has an id not found in the collection', done => {
      collection.fromJSON({ _id: 'myId', foo: 21 }).then(res => {
        expect(res).toEqual({ _id: 'myId', foo: 21 });
        done();
      });
    });

    it('Should return a new model when data has an id not found in the collection', done => {
      collection.fromJSON({ _id: 'myId', foo: 21 }, { ignoreCollection: true })
        .then(res => {
          expect(res).toEqual({ _id: 'myId', foo: 21 });
          expect(collection.collection).toEqual([]);
          done();
        });
    });

    it('Should return a existing model, _syncFrom depending on isComplete, if data id is found in the collection', done => {
      let model = {
        _id: 'myId',
        foo: 42
      };
      collection.collection.push(model);

      collection.fromJSON({ _id: 'myId' }).then(res => {
        expect(res).toEqual(model);
        done();
      });
    });
  });

  describe('.toJSON()', () => {
    let model;
    let collection;

    beforeEach(() => {
      let creator = (data) => data;
      model = { _id: 'fakeId', other: 'field' };
      collection = new Collection();
      config.registerCollection('myKey', collection, 'default/route/', creator);
    });

    it('Should return the model when the model doesn\'t have a toJSON method', () => {
      let result = collection.toJSON(model);
      expect(result).toEqual(model);
    });

    it('Should call the toJSON method of a model', () => {
      model.toJSON = () => 'myFakeJSON';

      let result = collection.toJSON(model);
      expect(result).toBe('myFakeJSON');
{'fraction_non_alphanumeric': 0.16683891923815147, 'fraction_numerical': 0.0029529012254540087, 'mean_word_length': 2.848863636363636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, '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': '942989', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1884, 'n_words': 483}