-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
95 lines (75 loc) · 2.88 KB
/
test.js
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
"use strict"
var assert = require('chai').assert
var StorageShim = require('./index')
describe('StorageShim', function () {
var instance
beforeEach(function () {
instance = new StorageShim()
})
it('.length returns the number of keys', function () {
assert.strictEqual(instance.length, 0)
instance.setItem('1', 1)
assert.strictEqual(instance.length, 1)
instance.setItem('2', 2)
assert.strictEqual(instance.length, 2)
})
it('#key(n) returns the nth key', function () {
instance.setItem('1', 1)
assert.strictEqual(instance.key(0), '1')
assert.strictEqual(instance.key(1), null)
})
it('#setItem(key, value) sets an item that can be retrieved by #getItem(key)', function () {
instance.setItem('1', 'foobar')
assert.strictEqual(instance.getItem('1'), 'foobar')
})
it('#setItem(key, value) sets an item that can be retrieved by property access', function () {
instance.setItem('foo', 'bar')
assert.strictEqual(instance.foo, 'bar')
})
it('#getItem(key) returns items set using property access', function () {
instance.foo = 'bar'
assert.strictEqual(instance.getItem('foo'), 'bar')
})
it('#getItem(key) returns null if key does not exist', function () {
assert.strictEqual(instance.getItem('1'), null)
})
it('#clear() deletes all items', function () {
instance.setItem('1', 'foobar')
instance.clear()
assert.strictEqual(instance.getItem('1'), null)
})
it('#removeItem(key) deletes the item for that key', function () {
instance.setItem('1', 'foobar')
instance.removeItem('1')
assert.strictEqual(instance.getItem('1'), null)
})
it('empty key works', function () {
instance.setItem('', 'foobar')
assert.strictEqual(instance.getItem(''), 'foobar')
assert.strictEqual(instance.key(0), '')
})
it('for in iteration works', function () {
instance.setItem('foo', 'bar')
for (var key in instance) {
assert.strictEqual(key, 'foo')
}
})
it('Object.keys works', function () {
instance.setItem('foo', 'bar')
assert.strictEqual(Object.keys(instance)[0], 'foo')
})
const proxyTestCases = [
{value: 123, expected: '123'},
{value: true, expected: 'true'},
{value: null, expected: 'null'},
{value: undefined, expected: 'undefined'},
{value: {some: 'obj'}, expected: '[object Object]'},
{value: {toString: () => "object with toString method()"}, expected: "object with toString method()"}
]
proxyTestCases.forEach(function ({value, expected}) {
it(`correctly proxies direct property sets for type "${typeof value}"`, function () {
instance.foo = value
assert.strictEqual(instance.foo, expected)
})
})
})