测试版
This commit is contained in:
14
node_modules/store/tests/bugs/all.js
generated
vendored
Normal file
14
node_modules/store/tests/bugs/all.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
test.group('bugs', function() {
|
||||
test('gh-215: "Expire plugin doesn\'t factor custom namespaces"', function() {
|
||||
require('./gh-215')
|
||||
})
|
||||
test('gh-235: "Expire and Events plugins conflict with each other"', function() {
|
||||
require('./gh-235')
|
||||
})
|
||||
test('gh-236: "No supported storage has been added"', function() {
|
||||
require('./gh-236')
|
||||
})
|
||||
test('gh-239: "No supported storage has been added! Add one"', function() {
|
||||
require('./gh-239')
|
||||
})
|
||||
})
|
||||
26
node_modules/store/tests/bugs/gh-215.js
generated
vendored
Normal file
26
node_modules/store/tests/bugs/gh-215.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
/* eslint semi: "off", indent: "off" */
|
||||
// https://github.com/marcuswestin/store.js/issues/215
|
||||
|
||||
var engine = require('../../src/store-engine');
|
||||
var storages = [require('../../storages/memoryStorage')];
|
||||
var plugins = [require('../../plugins/expire')];
|
||||
|
||||
const store1 = engine.createStore(storages, plugins, '');
|
||||
const store2 = store1.namespace('store2')
|
||||
const store3 = store1.namespace('store3')
|
||||
|
||||
var time = Math.floor(new Date().getTime() / 10) * 10
|
||||
var expiration1 = time + 1001
|
||||
var expiration2 = null
|
||||
var expiration3 = time + 3003
|
||||
|
||||
var key = 'foo'
|
||||
var val = 'bar'
|
||||
|
||||
store1.set(key, val, expiration1)
|
||||
store2.set(key, val, expiration2)
|
||||
store3.set(key, val, expiration3)
|
||||
|
||||
assert(store1.getExpiration(key) == expiration1)
|
||||
assert(store2.getExpiration(key) == expiration2)
|
||||
assert(store3.getExpiration(key) == expiration3)
|
||||
14
node_modules/store/tests/bugs/gh-235.js
generated
vendored
Normal file
14
node_modules/store/tests/bugs/gh-235.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
/* eslint semi: "off", indent: "off" */
|
||||
// https://github.com/marcuswestin/store.js/issues/235
|
||||
|
||||
var engine = require('../../src/store-engine');
|
||||
|
||||
const store = engine.createStore(
|
||||
[ require('../../storages/localStorage'), require('../../storages/memoryStorage') ],
|
||||
[ require('../../plugins/expire'), require('../../plugins/events') ]
|
||||
);
|
||||
|
||||
store.set('foo', 'bar', new Date().getTime() - 1);
|
||||
store.set('foo', 'bar');
|
||||
|
||||
store.set('foo', 'bar');
|
||||
5
node_modules/store/tests/bugs/gh-236.js
generated
vendored
Normal file
5
node_modules/store/tests/bugs/gh-236.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/* eslint semi: "off", no-unused-vars: "off" */
|
||||
|
||||
var store = require('../..');
|
||||
|
||||
const token = store.get('token'); // or set ...
|
||||
5
node_modules/store/tests/bugs/gh-239.js
generated
vendored
Normal file
5
node_modules/store/tests/bugs/gh-239.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/* eslint semi: "off", no-unused-vars: "off" */
|
||||
|
||||
var store = require('../..');
|
||||
const set = (key, value) => store.set(key, value);
|
||||
const get = key => store.get(key);
|
||||
157
node_modules/store/tests/tests.js
generated
vendored
Normal file
157
node_modules/store/tests/tests.js
generated
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
var tinytest = require('tinytest')
|
||||
|
||||
tinytest.hijackConsoleLog()
|
||||
|
||||
var { createStore } = require('../src/store-engine')
|
||||
var { each } = require('../src/util')
|
||||
var storages = require('../storages/all')
|
||||
var allPluginTests = require('../plugins/all_tests')
|
||||
|
||||
module.exports = {
|
||||
output:null,
|
||||
outputError:null,
|
||||
runTests: runTests,
|
||||
failed:false
|
||||
}
|
||||
|
||||
function runTests() {
|
||||
setupEngineTests()
|
||||
each(storages, function(storage) {
|
||||
test.group(storage.name, function() {
|
||||
if (!_checkEnabled(storage)) {
|
||||
test.skip('disabled')
|
||||
}
|
||||
test('Storage tests', function() {
|
||||
var store = createStore([storage])
|
||||
runStorageTests(store)
|
||||
})
|
||||
each(allPluginTests, function(pluginTest, pluginName) {
|
||||
var plugin = pluginTest.plugin
|
||||
test.group('plugin: '+pluginName, function() {
|
||||
var store = createStore([storage], [plugin])
|
||||
pluginTest.setup(store)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
require('./bugs/all')
|
||||
|
||||
tinytest.runTests({
|
||||
failFast: false
|
||||
})
|
||||
}
|
||||
|
||||
function _checkEnabled(storage) {
|
||||
if (!storage) {
|
||||
print('Skip unsupported storage:', storage.name)
|
||||
return false
|
||||
}
|
||||
var store = createStore([storage])
|
||||
if (!store.enabled) {
|
||||
print('Skip disabled storage:', storage.name)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function setupEngineTests(store) {
|
||||
test('Addon super_fn args', function() {
|
||||
function underlyingPlugin() {
|
||||
return {
|
||||
set: function(super_fn, key, val, customArg1, customArg2) {
|
||||
assert(key == 'key'+'appended')
|
||||
assert(val == 'val')
|
||||
assert(customArg1 == 'overridden-customArg1')
|
||||
assert(customArg2 == 'customArg2')
|
||||
calls++
|
||||
}
|
||||
}
|
||||
}
|
||||
function overlyingPlugin() {
|
||||
return {
|
||||
set: function(super_fn, key, val) {
|
||||
super_fn(key+'appended', val, 'overridden-customArg1')
|
||||
calls++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var store = createStore(storages.memoryStorage, [underlyingPlugin, overlyingPlugin])
|
||||
var calls = 0
|
||||
store.set('key', 'val', 'customArg1', 'customArg2')
|
||||
assert(calls == 2)
|
||||
})
|
||||
}
|
||||
|
||||
function runStorageTests(store) {
|
||||
assert(store.enabled && store.enabled, "store should be enabled")
|
||||
store.clearAll()
|
||||
|
||||
store.get('unsetValue') // see https://github.com/marcuswestin/store.js/issues/63
|
||||
|
||||
store.set('foo', 'bar')
|
||||
assert(store.get('foo') == 'bar', "stored key 'foo' not equal to stored value 'bar'")
|
||||
|
||||
store.remove('foo')
|
||||
assert(store.get('foo') === undefined, "removed key 'foo' not undefined")
|
||||
|
||||
assert(store.get('foo') === undefined, "key 'foo' exists when it shouldn't")
|
||||
assert(store.set('foo','value') == 'value', "store#set returns the stored value")
|
||||
assert(store.get('foo') !== undefined, "key 'foo' doesn't exist when it should")
|
||||
|
||||
store.set('foo', 'bar1')
|
||||
store.set('foo', 'bar2')
|
||||
assert(store.get('foo') == 'bar2', "key 'foo' is not equal to second value set 'bar2'")
|
||||
|
||||
store.set('foo', 'bar')
|
||||
store.set('bar', 'foo')
|
||||
store.remove('foo')
|
||||
assert(store.get('foo') === undefined, "key 'foo' exists when it shouldn't")
|
||||
assert(store.get('bar') == 'foo', "removing key 'foo' also removed key 'bar'")
|
||||
|
||||
store.set('foo', 'bar')
|
||||
store.set('bar', 'foo')
|
||||
store.clearAll()
|
||||
assert(store.get('foo') === undefined && store.get('bar') === undefined, "keys foo and bar not cleared after store cleared")
|
||||
|
||||
assert(store.get('defaultVal', 123) == 123, "store.get should return default value")
|
||||
|
||||
store.set('foo', { name: 'marcus', arr: [1,2,3] })
|
||||
assert(typeof store.get('foo') == 'object', "type of stored object 'foo' is not 'object'")
|
||||
assert(store.get('foo') instanceof Object, "stored object 'foo' is not an instance of Object")
|
||||
assert(store.get('foo').name == 'marcus', "property 'name' of stored object 'foo' is not 'marcus'")
|
||||
assert(store.get('foo').arr instanceof Array, "Array property 'arr' of stored object 'foo' is not an instance of Array")
|
||||
assert(store.get('foo').arr.length == 3, "The length of Array property 'arr' stored on object 'foo' is not 3")
|
||||
|
||||
store.remove('circularReference')
|
||||
var circularOne = {}
|
||||
var circularTwo = { one:circularOne }
|
||||
circularOne.two = circularTwo
|
||||
var threw = false
|
||||
try { store.set('circularReference', circularOne) }
|
||||
catch(e) { threw = true }
|
||||
assert(threw, "storing object with circular reference did not throw")
|
||||
assert(!store.get('circularReference'), "attempting to store object with circular reference which should have faile affected store state")
|
||||
|
||||
// If plain local storage was used before store.js, we should attempt to JSON.parse them into javascript values.
|
||||
// Store values using vanilla localStorage, then read them out using store.js
|
||||
var promoteValues = {
|
||||
'int' : 42,
|
||||
'bool' : true,
|
||||
'float' : 3.141592653,
|
||||
'string' : "Don't Panic",
|
||||
'odd_string' : "{ZYX'} abc:;::)))"
|
||||
}
|
||||
for (var key in promoteValues) {
|
||||
store.storage.write(key, promoteValues[key])
|
||||
assert(store.get(key) == promoteValues[key], key+" was not correctly promoted to valid JSON")
|
||||
store.remove(key)
|
||||
}
|
||||
store.clearAll()
|
||||
var count = 0
|
||||
store.each(function() {
|
||||
count += 1
|
||||
})
|
||||
assert(count === 0)
|
||||
}
|
||||
24
node_modules/store/tests/util.js
generated
vendored
Normal file
24
node_modules/store/tests/util.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
module.exports = {
|
||||
deepEqual: deepEqual
|
||||
}
|
||||
|
||||
function deepEqual(a,b) {
|
||||
if (typeof a != typeof b) {
|
||||
return false
|
||||
}
|
||||
if (typeof a != 'object') {
|
||||
return a === b
|
||||
}
|
||||
var key
|
||||
for (key in a) {
|
||||
if (!deepEqual(a[key], b[key])) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for (key in b) {
|
||||
if (!deepEqual(b[key], a[key])) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user