<commit_before>import expect from 'expect';

import Entity from '../src/entity';

describe('Entity', () => {
  describe('#get', () => {
    it('should return a component');
    it('should throw error when component is invalid');
    it('should throw error when entity is invalid');
  });
  describe('#set', () => {
    it('should set a component');
    it('should prevent overriding id component');
    it('should throw error when component is invalid');
    it('should throw error when entity is invalid');
    it('should emit an entity event');
    it('should emit a component event');
  });
  describe('#remove', () => {
    it('should remove a component');
    it('should prevent removing id component');
    it('should throw error when component is invalid');
    it('should throw error when entity is invalid');
    it('should emit an entity event');
    it('should emit a component event');
  });
});<commit_msg>Implement entity tests<commit_after>import expect from 'expect';

import Engine from '../src/engine';
import Entity from '../src/entity';

describe('Entity', () => {
  let engine, entity;
  // Action create functions.
  const set = (entity, key, value) => ({
    type: 'set',
    payload: {
      entity, key, value
    },
    meta: {}
  });
  const remove = (entity, key) => ({
    type: 'remove',
    payload: {
      entity, key
    },
    meta: {}
  });
  // Since Entity works with the engine, we need to initialize the engine too.
  beforeEach('initialize engine with entities', () => {
    engine = new Engine([], [
      // We can't directly mutate the engine; we need to use a system
      // to mutate it.
      (engine, action) => {
        const { type, payload } = action;
        if (type === 'set') {
          const { entity, key, value } = payload;
          entity.set(key, value);
        }
        if (type === 'remove') {
          const { entity, key } = payload;
          entity.remove(key);
        }
      }
    ], ['test'], {
      id: {
        1: 1
      },
      test: {
        1: 'sleepy'
      }
    });
    entity = engine.get(1);
  });
  describe('#get', () => {
    it('should return a component', () => {
      expect(entity.get('test')).toBe('sleepy');
    });
    it('should throw error when component is invalid', () => {
      expect(() => entity.get('never')).toThrow();
    });
    it('should throw error when entity is invalid', () => {
      entity = new Entity(engine, 32767);
      expect(() => entity.get('test')).toThrow();
    });
  });
  describe('#set', () => {
    it('should set a component', () => {
      engine.dispatch(set(entity, 'test', '2spooky4me'));
      expect(entity.get('test')).toBe('2spooky4me');
    });
    it('should prevent overriding id component', () => {
      expect(() => engine.dispatch(set(entity, 'id', 'nope'))).toThrow();
    });
    it('should throw error when component is invalid', () => {
      expect(() => engine.dispatch(set(entity, 'tera', 'byte'))).toThrow();
    });
    it('should throw error when entity is invalid', () => {
      entity = new Entity(engine, 32767);
      expect(() => engine.dispatch(set(entity, 'test', 'nevermind'))).toThrow();
    });
    it('should emit an event', () => {
      // Here, Set up the observer to verify if the event has succeeded.
      let count = 0;
      engine.observe('test', () => count++);
      entity.observe(() => count++);
      engine.dispatch(set(entity, 'test', 'probably'));
      expect(count).toBe(2);
    });
    it('should have previous state in events', () => {
      engine.observe('test', event => {
        expect(event.type).toBe('component');
        expect(event.key).toBe('test');
        expect(event.values[1]).toBe('sleepy');
      });
      entity.observe(event => {
        expect(event.type).toBe('entity');
        expect(event.key).toBe(1);
        expect(event.values['test']).toBe('sleepy');
      });
      engine.dispatch(set(entity, 'test', 'yes'));
    });
  });
  describe('#remove', () => {
    it('should remove a component', () => {
      engine.dispatch(remove(entity, 'test'));
      expect(entity.get('test')).toEqual(undefined);
    });
    it('should prevent removing id component', () => {
      expect(() => engine.dispatch(remove(entity, 'id'))).toThrow();
    });
    it('should throw error when component is invalid', () => {
      expect(() => engine.dispatch(remove(entity, 'giga'))).toThrow();
    });
    it('should throw error when entity is invalid', () => {
      entity = new Entity(engine, 32767);
      expect(() => engine.dispatch(remove(entity, 'test'))).toThrow();
    });
    it('should emit an event', () => {
      // Here, Set up the observer to verify if the event has succeeded.
      let count = 0;
      engine.observe('test', () => count++);
      entity.observe(() => count++);
      engine.dispatch(remove(entity, 'test'));
      expect(count).toBe(2);
    });
    it('should have previous state in events', () => {
      engine.observe('test', event => {
        expect(event.type).toBe('component');
        expect(event.key).toBe('test');
        expect(event.values[1]).toBe('sleepy');
      });
      entity.observe(event => {
        expect(event.type).toBe('entity');
        expect(event.key).toBe(1);
        expect(event.values['test']).toBe('sleepy');
      });
      engine.dispatch(remove(entity, 'test'));
    });
  });
});
{'fraction_non_alphanumeric': 0.1928451648965903, 'fraction_numerical': 0.00577603875535681, 'mean_word_length': 2.7564730580825754, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 46, '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': '4389951', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1543, 'n_words': 407}