local_storage.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. window.onload = function() {
  2. QUnit.test('Test item removal from local storage', assert => {
  3. var key = 'test_key_1'
  4. var value = 'test_value_1'
  5. App.LocalStorage.set(key, value)
  6. assert.equal(App.LocalStorage.get(key), value)
  7. App.LocalStorage.delete(key)
  8. assert.equal(App.LocalStorage.get(key), undefined)
  9. });
  10. QUnit.test('Test user-specific item removal from local storage', assert => {
  11. var key = 'test_key_2'
  12. var value = 'test_value_2'
  13. var user_id = 2
  14. App.LocalStorage.set(key, value, user_id)
  15. assert.equal(App.LocalStorage.get(key, user_id), value)
  16. App.LocalStorage.delete(key, user_id)
  17. assert.equal(App.LocalStorage.get(key, user_id), undefined)
  18. });
  19. QUnit.test('Test key lookup', assert => {
  20. App.LocalStorage.clear()
  21. var key = 'test_key_3'
  22. var value = 'test_value_3'
  23. var user_id = 2
  24. var alt_key = 'test_alt_key_3'
  25. // verify no keys initially
  26. assert.equal(App.LocalStorage.keys().length, 0)
  27. App.LocalStorage.set(key, value, user_id)
  28. // has 1 key in total
  29. assert.equal(App.LocalStorage.keys().length, 1)
  30. // doesn't return anything with wrong prefix
  31. assert.equal(App.LocalStorage.keys('a').length, 0)
  32. // doesn't return anything since user id not given
  33. assert.equal(App.LocalStorage.keys('test').length, 0)
  34. // correct
  35. assert.equal(App.LocalStorage.keys('test', user_id).length, 1)
  36. // verify value
  37. assert.equal(App.LocalStorage.keys('test', user_id)[0].match(key + '$'), key)
  38. App.LocalStorage.set(alt_key, value)
  39. // returns 1 key without user id
  40. assert.equal(App.LocalStorage.keys('test').length, 1)
  41. assert.equal(App.LocalStorage.keys('test')[0], alt_key)
  42. });
  43. }