<commit_before>{sinon, _} = require 'shared/specs/helpers'
{ autorun } = require 'mobx';
UiSettings = require 'model/ui-settings'
Networking = require 'model/networking'

jest.useFakeTimers();

describe 'UiSettings', ->

  beforeEach ->
    sinon.stub(Networking, 'perform').returns(
      then: (fn) -> fn({})
    )


  afterEach ->
    UiSettings._reset()
    Networking.perform.restore()

  it 'remembers initialized values', ->
    UiSettings.initialize('one': 1, 'two':'II')
    expect(UiSettings.get('one')).to.equal(1)
    expect(UiSettings.get('two')).to.equal('II')
    undefined

  it 'saves when set', ->
    initialSetting = {'one': 1, 'two':'II'}
    UiSettings.initialize(initialSetting)
    UiSettings.set(one: 'five')
    jest.runAllTimers()
    expect(Networking.perform).to.have.been.called
    expect(Networking.perform.lastCall.args[0].data?.ui_settings).to.eql(
      one: 'five', two: 'II'
    )
    undefined

  it 'groups saves together', ->
    initialSetting = {one: 18, two:'III', deep: {key: 'value', bar: 'bz'}}
    UiSettings.initialize(initialSetting)
    UiSettings.set(one: 'five')
    UiSettings.set(one: 'six', deep: {bar: 'foo'})
    UiSettings.set(one: 'seven')
    jest.runAllTimers()
    expect(Networking.perform).to.have.been.calledOnce

    expect(Networking.perform.lastCall.args[0].data?.ui_settings).to.eql(
      one: 'seven', two: 'III', deep: { bar: 'foo'}
    )
    undefined

  it 'can set with key and id', ->
    initialSetting = {one: 18, two:'III', deep: {key: 'value', bar: 'bz'}}<commit_msg>use runOnlyPendingTimers which works better with debounce<commit_after>{sinon, _} = require 'shared/specs/helpers'
{ autorun } = require 'mobx'
saveSettingsMock = jest.fn()
jest.mock('lodash/debounce', -> jest.fn(-> saveSettingsMock ))

UiSettings = require 'model/ui-settings'
Networking = require 'model/networking'
mockedDebounce = require 'lodash/debounce'



describe 'UiSettings', ->

  beforeEach ->
    saveSettingsMock.mockReset()
    sinon.stub(Networking, 'perform').returns(
      then: (fn) -> fn({})
    )

  afterEach ->
    UiSettings._reset()
    Networking.perform.restore()

  it 'remembers initialized values', ->
    UiSettings.initialize('one': 1, 'two':'II')
    expect(UiSettings.get('one')).to.equal(1)
    expect(UiSettings.get('two')).to.equal('II')
    undefined

  it 'saves when set', ->
    initialSetting = {'one': 1, 'two':'II'}
    UiSettings.initialize(initialSetting)

    UiSettings.set(one: 'five')
    expect(saveSettingsMock).toHaveBeenCalledTimes(1)

  it 'groups saves together', ->
    initialSetting = {one: 18, two:'III', deep: {key: 'value', bar: 'bz'}}
    UiSettings.initialize(initialSetting)
    UiSettings.set(one: 'five')
    UiSettings.set(one: 'six', deep: {bar: 'foo'})
    UiSettings.set(one: 'seven')
    expect(saveSettingsMock).toHaveBeenCalledTimes(3)
    undefined

  it 'can set with key and id', ->
    initialSetting = {one: 18, two:'III', deep: {key: 'value', bar: 'bz'}}
{'fraction_non_alphanumeric': 0.17197452229299362, 'fraction_numerical': 0.006034193764666443, 'mean_word_length': 3.9078947368421053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '5439660', 'n_tokens_mistral': 1049, 'n_tokens_neox': 995, 'n_words': 203}