localStorage.spec.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* global localStorage */
  2. import test from 'ava'
  3. import './localStorage'
  4. test('methods should work', (t) => {
  5. // can't make assuptions about key positioning
  6. localStorage.setItem('a', 1)
  7. t.is(localStorage.key(0), 'a')
  8. localStorage.setItem('b', '2')
  9. t.is(localStorage.getItem('a'), '1')
  10. t.is(localStorage.getItem('b'), '2')
  11. t.is(localStorage.length, 2)
  12. t.is(localStorage['c'], undefined)
  13. t.is(localStorage.getItem('c'), null)
  14. localStorage.setItem('c')
  15. t.is(localStorage.getItem('c'), 'undefined')
  16. t.is(localStorage.length, 3)
  17. localStorage.removeItem('c')
  18. t.is(localStorage.getItem('c'), null)
  19. t.is(localStorage.length, 2)
  20. localStorage.clear()
  21. t.is(localStorage.getItem('a'), null)
  22. t.is(localStorage.getItem('b'), null)
  23. t.is(localStorage.length, 0)
  24. })
  25. test('error throwing', (t) => {
  26. t.throws(() => {
  27. localStorage.key()
  28. }, /Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present\./)
  29. })
  30. test('proxy should work', (t) => {
  31. t.is(localStorage.length, 0)
  32. localStorage.a = {}
  33. t.is(localStorage.a, '[object Object]')
  34. localStorage.c = 1
  35. const obj = {}
  36. localStorage[obj] = 'key gets stringified'
  37. t.is(localStorage['[object Object]'], 'key gets stringified')
  38. t.is(localStorage.c, '1')
  39. t.is(localStorage.length, 3)
  40. localStorage.length = 0
  41. t.is(localStorage.length, 3)
  42. localStorage.key = 'only an ass**** would do this'
  43. t.is(localStorage.length, 3)
  44. t.is(localStorage.key, 'only an ass**** would do this')
  45. })