<commit_before>import { Map, fromJS } from 'immutable';
import reducer, { broadcast } from '../messages';
import * as actions from '../actions';

describe('reducers/messages', () => {
  it('adds the message on ADD_MESSAGE', () => {
    const state = reducer(undefined, {
      type: actions.ADD_MESSAGE,
      server: 'srv',
      tab: '#chan1',
      message: {
        from: 'foo',
        content: 'msg'
      }
    });

    expect(state.toJS()).toMatchObject({
      srv: {
        '#chan1': [{
          from: 'foo',
          content: 'msg'
        }]
      }
    });
  });

  it('adds all the messsages on ADD_MESSAGES', () => {
    const state = reducer(undefined, {
      type: actions.ADD_MESSAGES,
      server: 'srv',
      tab: '#chan1',
      messages: [
        {
          from: 'foo',
          content: 'msg'
        }, {
          from: 'bar',
          content: 'msg'
        }, {
          tab: '#chan2',
          from: 'foo',
          content: 'msg'
        }
      ]
    });

    expect(state.toJS()).toMatchObject({
      srv: {
        '#chan1': [
          {
            from: 'foo',
            content: 'msg'
          }, {
            from: 'bar',
            content: 'msg'
          }
        ],
        '#chan2': [{
          from: 'foo',
          content: 'msg'
        }]
      }
    });
  });

  it('handles prepending of messages on ADD_MESSAGES', () => {
    let state = fromJS({
      srv: {
        '#chan1': [{ id: 0 }]
      }
    });

    state = reducer(state, {
      type: actions.ADD_MESSAGES,
      server: 'srv',
      tab: '#chan1',
      prepend: true,
      messages: [{ id: 1 }, { id: 2 }]
    });

    expect(state.toJS()).toMatchObject({
      srv: {
        '#chan1': [{ id: 1 }, { id: 2 }, { id: 0 }]
      }
    });
  });

  it('adds messages to the correct tabs when broadcasting', () => {
    let state = {
      environment: Map({
        charWidth: 0,
        wrapWidth: 0
      })
    };

    const thunk = broadcast('test', 'srv', ['#chan1', '#chan3']);
    thunk(
      action => { state.messages = reducer(undefined, action); },
      () => state
    );

    const messages = state.messages.toJS();

    expect(messages.srv).not.toHaveProperty('srv');
    expect(messages.srv['#chan1']).toHaveLength(1);
    expect(messages.srv['#chan1'][0].content).toBe('test');
    expect(messages.srv['#chan3']).toHaveLength(1);
    expect(messages.srv['#chan3'][0].content).toBe('test');
  });

  it('deletes all messages related to server when disconnecting', () => {
    let state = fromJS({
      srv: {
        '#chan1': [
          { content: 'msg1' },
          { content: 'msg2' }
        ],
        '#chan2': [<commit_msg>Fix message reducer tests<commit_after>import { Map, fromJS } from 'immutable';
import reducer, { broadcast } from '../messages';
import * as actions from '../actions';
import appReducer from '../app';

describe('reducers/messages', () => {
  it('adds the message on ADD_MESSAGE', () => {
    const state = reducer(undefined, {
      type: actions.ADD_MESSAGE,
      server: 'srv',
      tab: '#chan1',
      message: {
        from: 'foo',
        content: 'msg'
      }
    });

    expect(state.toJS()).toMatchObject({
      srv: {
        '#chan1': [{
          from: 'foo',
          content: 'msg'
        }]
      }
    });
  });

  it('adds all the messsages on ADD_MESSAGES', () => {
    const state = reducer(undefined, {
      type: actions.ADD_MESSAGES,
      server: 'srv',
      tab: '#chan1',
      messages: [
        {
          from: 'foo',
          content: 'msg'
        }, {
          from: 'bar',
          content: 'msg'
        }, {
          tab: '#chan2',
          from: 'foo',
          content: 'msg'
        }
      ]
    });

    expect(state.toJS()).toMatchObject({
      srv: {
        '#chan1': [
          {
            from: 'foo',
            content: 'msg'
          }, {
            from: 'bar',
            content: 'msg'
          }
        ],
        '#chan2': [{
          from: 'foo',
          content: 'msg'
        }]
      }
    });
  });

  it('handles prepending of messages on ADD_MESSAGES', () => {
    let state = fromJS({
      srv: {
        '#chan1': [{ id: 0 }]
      }
    });

    state = reducer(state, {
      type: actions.ADD_MESSAGES,
      server: 'srv',
      tab: '#chan1',
      prepend: true,
      messages: [{ id: 1 }, { id: 2 }]
    });

    expect(state.toJS()).toMatchObject({
      srv: {
        '#chan1': [{ id: 1 }, { id: 2 }, { id: 0 }]
      }
    });
  });

  it('adds messages to the correct tabs when broadcasting', () => {
    let state = {
      app: appReducer(undefined, { type: '' })
    };

    const thunk = broadcast('test', 'srv', ['#chan1', '#chan3']);
    thunk(
      action => { state.messages = reducer(undefined, action); },
      () => state
    );

    const messages = state.messages.toJS();

    expect(messages.srv).not.toHaveProperty('srv');
    expect(messages.srv['#chan1']).toHaveLength(1);
    expect(messages.srv['#chan1'][0].content).toBe('test');
    expect(messages.srv['#chan3']).toHaveLength(1);
    expect(messages.srv['#chan3'][0].content).toBe('test');
  });

  it('deletes all messages related to server when disconnecting', () => {
    let state = fromJS({
      srv: {
        '#chan1': [
          { content: 'msg1' },
          { content: 'msg2' }
        ],
        '#chan2': [
{'fraction_non_alphanumeric': 0.1839613885279376, 'fraction_numerical': 0.011137924633376647, 'mean_word_length': 1.7801857585139318, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4437042', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1753, 'n_words': 362}