<commit_before>Listnr = @Listnr
buster = @buster

assert = buster.assert
refute = buster.refute

createEl = (tag) ->
  document.createElement(tag)

triggerCombo = (el, combo) ->
  event = document.createEvent('Event')
  event.initEvent('keypress', true, true)
  event.keyCode = combo.charCodeAt(0)
  el.dispatchEvent(event)

buster.testCase 'Listnr',

  'can instantiate': ->
    listnr = new Listnr()
    assert(listnr instanceof Listnr)

  '.map':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'returns self': ->
      assert.same(@listnr.map(), @listnr)

    'does not call the handler': ->
      combo = 'a'
      spy = @spy()

      @listnr.map(combo, spy)

      refute.called(spy)

    'creates a new listener which is triggered on keyboard events': ->
      combo = 'a'
      spy = @spy()

      @listnr.map(combo, spy)
      triggerCombo(@el, combo)

      assert.calledOnceWith(spy, combo)
      assert.calledOn(spy, @listnr)

    'only triggers on match': ->
      spy = @spy()

      @listnr.map('a', spy)
      triggerCombo(@el, 'b')

      refute.called(spy)

    'can add help text': ->
      spy = @spy()

      @listnr.map('a', 'Mapping description', spy)
      triggerCombo(@el, 'a')

      assert.calledOnceWith(spy, 'a')

  '.unmap':

    setUp: ->
      @el = createEl('div')
      @combo = 'a'
      @keySpy = @spy()
      @listnr = new Listnr(el: @el)
        .map(@combo, @keySpy)

    'returns self': ->
      assert.same(@listnr.unmap(), @listnr)

    'removes listener': ->
      @listnr.unmap(@combo)

      triggerCombo(@el, @combo)

      refute.called(@keySpy)

  '.addContext':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'adds a new context of mappings': ->
      ctxHandler = @spy()
      defaultHandler = @spy()

      @listnr
        .map('a', defaultHandler)
        .addContext('menu')
        .map('a', ctxHandler)
        .activate()

      triggerCombo(@el, 'a')

      assert.calledOnce(ctxHandler)
      refute.called(defaultHandler)

  '.activate':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'returns self': ->
      assert.same(@listnr.activate(), @listnr)

    'can activate context by name': ->
      spy = @spy()
      @listnr
        .addContext('context')
        .map('a', spy)

      @listnr.activate('context')
      triggerCombo(@el, 'a')

      assert.calledOnce(spy)

  '.reset':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'returns self': ->
      assert.same(@listnr.reset(), @listnr)

    'returns to the default context': ->
      ctxSpy = @spy()
      defSpy = @spy()

      @listnr
        .map('a', defSpy)
        .addContext('context')
        .map('a', ctxSpy)
        .activate()

      @listnr.reset()
      triggerCombo(@el, 'a')

      assert.calledOnce(defSpy)
      refute.called(ctxSpy)

  '.default':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'returns self': ->
      assert.same(@listnr.default(), @listnr)

    'adds a default handler for all non-matching combos': ->
      spy = @spy()

      @listnr.default(spy)
      triggerCombo(@el, 'a')

      assert.calledOnceWith(spy, 'a')

  '.help':

    'returns help text': ->
      help = new Listnr()
        .map('a', 'Mapping for a', ->)
        .map('b', ->)
        .map('c', 'Mapping for c', ->)
        .help()

      assert.equals help,
        'a': 'Mapping for a'
        'b': null
        'c': 'Mapping for c'<commit_msg>Add test for help returns active context help<commit_after>Listnr = @Listnr
buster = @buster

assert = buster.assert
refute = buster.refute

createEl = (tag) ->
  document.createElement(tag)

triggerCombo = (el, combo) ->
  event = document.createEvent('Event')
  event.initEvent('keypress', true, true)
  event.keyCode = combo.charCodeAt(0)
  el.dispatchEvent(event)

buster.testCase 'Listnr',

  'can instantiate': ->
    listnr = new Listnr()
    assert(listnr instanceof Listnr)

  '.map':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'returns self': ->
      assert.same(@listnr.map(), @listnr)

    'does not call the handler': ->
      combo = 'a'
      spy = @spy()

      @listnr.map(combo, spy)

      refute.called(spy)

    'creates a new listener which is triggered on keyboard events': ->
      combo = 'a'
      spy = @spy()

      @listnr.map(combo, spy)
      triggerCombo(@el, combo)

      assert.calledOnceWith(spy, combo)
      assert.calledOn(spy, @listnr)

    'only triggers on match': ->
      spy = @spy()

      @listnr.map('a', spy)
      triggerCombo(@el, 'b')

      refute.called(spy)

    'can add help text': ->
      spy = @spy()

      @listnr.map('a', 'Mapping description', spy)
      triggerCombo(@el, 'a')

      assert.calledOnceWith(spy, 'a')

  '.unmap':

    setUp: ->
      @el = createEl('div')
      @combo = 'a'
      @keySpy = @spy()
      @listnr = new Listnr(el: @el)
        .map(@combo, @keySpy)

    'returns self': ->
      assert.same(@listnr.unmap(), @listnr)

    'removes listener': ->
      @listnr.unmap(@combo)

      triggerCombo(@el, @combo)

      refute.called(@keySpy)

  '.addContext':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'adds a new context of mappings': ->
      ctxHandler = @spy()
      defaultHandler = @spy()

      @listnr
        .map('a', defaultHandler)
        .addContext('menu')
        .map('a', ctxHandler)
        .activate()

      triggerCombo(@el, 'a')

      assert.calledOnce(ctxHandler)
      refute.called(defaultHandler)

  '.activate':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'returns self': ->
      assert.same(@listnr.activate(), @listnr)

    'can activate context by name': ->
      spy = @spy()
      @listnr
        .addContext('context')
        .map('a', spy)

      @listnr.activate('context')
      triggerCombo(@el, 'a')

      assert.calledOnce(spy)

  '.reset':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'returns self': ->
      assert.same(@listnr.reset(), @listnr)

    'returns to the default context': ->
      ctxSpy = @spy()
      defSpy = @spy()

      @listnr
        .map('a', defSpy)
        .addContext('context')
        .map('a', ctxSpy)
        .activate()

      @listnr.reset()
      triggerCombo(@el, 'a')

      assert.calledOnce(defSpy)
      refute.called(ctxSpy)

  '.default':

    setUp: ->
      @el = createEl('div')
      @listnr = new Listnr(el: @el)

    'returns self': ->
      assert.same(@listnr.default(), @listnr)

    'adds a default handler for all non-matching combos': ->
      spy = @spy()

      @listnr.default(spy)
      triggerCombo(@el, 'a')

      assert.calledOnceWith(spy, 'a')

  '.help':

    'returns help text': ->
      help = new Listnr()
        .map('a', 'Mapping for a', ->)
        .map('b', ->)
        .map('c', 'Mapping for c', ->)
        .help()

      assert.equals help,
        'a': 'Mapping for a'
        'b': null
        'c': 'Mapping for c'

    'returns help for the active context': ->
      help = new Listnr()
        .map('a', 'Mapping for a', ->)
        .addContext('context')
        .map('b', ->)
        .map('c', 'Mapping for c', ->)
        .help()

      assert.equals help,
        'b': null
        'c': 'Mapping for c'
{'fraction_non_alphanumeric': 0.1693548387096774, 'fraction_numerical': 0.00026881720430107527, 'mean_word_length': 2.135693215339233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, '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': '7452053', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2598, 'n_words': 578}