File: default_callbacks.spec.coffee

package info (click to toggle)
jquery-at.js 1.5.4%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 312 kB
  • sloc: makefile: 72
file content (123 lines) | stat: -rw-r--r-- 4,496 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

$inputor = null
app = null

describe "default callbacks", ->
  $ = jQuery
  callbacks = null
  text = null

  beforeEach ->
    loadFixtures("inputors.html")
    $inputor = $("#inputor").atwho at: "@", data: fixtures["names"]
    app = getAppOf $inputor

  beforeEach ->
    text = $.trim $inputor.text()
    callbacks = $.fn.atwho.default.callbacks
    app = $inputor.data("atwho")

  afterEach ->
    $inputor.atwho 'destroy'

  it "refactor the data before save", ->
    items = callbacks.beforeSave.call(app.controller(), fixtures["names"])
    expect(items).toContain({"name":"Jacob"})
    expect(items).toContain({"name":"Isabella"})

  it "should match the key word following @", ->
    query = callbacks.matcher.call(app, "@", text)
    expect(query).toBe("Jobs")

  it "should not match a space following @ if acceptSpaceBar flag omitted", ->
    $inputor = $("#inputor").atwho at: "@", data: fixtures["names"]
    text = $.trim $inputor.text()
    query = callbacks.matcher.call(app, "@", text)
    expect(query).toBe("Jobs")

  it "should not match a space following @ if acceptSpaceBar flag false", ->
    $inputor = $("#inputor").atwho at: "@", data: fixtures["names"], acceptSpaceBar: false
    text = $.trim $inputor.text()
    query = callbacks.matcher.call(app, "@", text, false, false)
    expect(query).toBe("Jobs")

  it "should match a space following @ if acceptSpaceBar flag set to true", ->
    $inputor = $("#inputor4").atwho at: "@", data: fixtures["names"], acceptSpaceBar: true
    text = $.trim $inputor.text()
    query = callbacks.matcher.call(app, "@", text, false, true)
    expect(query).toBe("Jobs Blobs")

  it "should match the key word fllowing @ with specials chars", ->
    $inputor = $("#special-chars").atwho at: "@", data: fixtures["names"]
    text = $.trim $inputor.text()

    query = callbacks.matcher.call(app, "@", text)
    expect(query).toBe(decodeURI("J%C3%A9r%C3%A9m%C3%BF"))

  it "can filter data", ->
    names = callbacks.beforeSave.call(app.controller(), fixtures["names"])
    names = callbacks.filter.call(app, "jo", names, "name")
    expect(names).toContain name: "Joshua"

  it "can filter numeric data", ->
    numerics = callbacks.beforeSave.call(app.controller(), fixtures["numerics"])
    numerics = callbacks.filter.call(app, "1", numerics, "name")
    expect(numerics).toContain name: 10

  it "request data from remote by ajax if set remoteFilter", ->
    remote_call = jasmine.createSpy("remote_call")
    $inputor.atwho
      at: "@"
      data: null,
      callbacks:
        remoteFilter: remote_call

    simulateTypingIn $inputor
    expect(remote_call).toHaveBeenCalled()

  it "can sort the data", ->
    names = callbacks.beforeSave.call(app.controller(), fixtures["names"])
    names = callbacks.sorter.call(app, "e", names, "name")
    expect(names[0].name).toBe 'Ethan'

  it "can sort numeric data", ->
    numerics = callbacks.beforeSave.call(app.controller(), fixtures["numerics"])
    numerics = callbacks.sorter.call(app, "1", numerics, "name")
    expect(numerics[0].name).toBe 13

  it "don't sort the data without a query", ->
    names = callbacks.beforeSave.call(app.controller(), fixtures["names"])
    names = callbacks.sorter.call(app, "", names, "name")
    expect(names[0]).toEqual({ name : 'Jacob' })

  it "can eval temple", ->
    map = {name: "username", nick: "nick_name"}
    tpl = '<li data-value="${name}">${nick}</li>'
    html = '<li data-value="username">nick_name</li>'

    result = callbacks.tplEval.call(app, tpl, map)
    expect(result).toBe(html)

  it "can evaluate template as a function", ->
    map = {name: "username", nick: "nick_name"}
    tpl = (map)-> '<li data-value="'+map.name+'">'+map.nick+'</li>'
    html = '<li data-value="username">nick_name</li>'
    result = callbacks.tplEval.call(app, tpl, map)
    expect(result).toBe(html)


  it "can highlight the query", ->
    html = '<li data-value="username">Ethan</li>'
    highlighted = callbacks.highlighter.call(app, html, "e")
    result = '<li data-value="username"> <strong>E</strong>than </li>'
    expect(highlighted).toBe(result)

  it "can insert the text which be choosed", ->
    spyOn(callbacks, "beforeInsert").and.callThrough()
    triggerAtwhoAt $inputor
    expect(callbacks.beforeInsert).toHaveBeenCalled()

  it "can adjust offset before reposition", ->
    spyOn(callbacks, "beforeReposition").and.callThrough()
    triggerAtwhoAt $inputor
    expect(callbacks.beforeReposition).toHaveBeenCalled()