localStorage.test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import localStorage from '../src/localStorage'
  2. let getStorageSync, setStorageSync, removeStorageSync, getStorageInfoSync, clearStorageSync
  3. beforeEach(() => {
  4. getStorageSync = setStorageSync = removeStorageSync = getStorageInfoSync = clearStorageSync = jest.fn()
  5. global.wx = {
  6. getStorageSync,
  7. setStorageSync,
  8. removeStorageSync,
  9. getStorageInfoSync,
  10. clearStorageSync
  11. }
  12. })
  13. describe('localStorage', () => {
  14. it('call getStorageInfoSync() with length', () => {
  15. wx.getStorageInfoSync = jest.fn(() => {
  16. return {
  17. keys: ['key0', 'key1', 'key2', 'key3', 'key4']
  18. }
  19. })
  20. expect(localStorage.length).toEqual(5)
  21. expect(wx.getStorageInfoSync).toHaveBeenCalledTimes(1)
  22. })
  23. it('call getStorageInfoSync() with key()', () => {
  24. wx.getStorageInfoSync = jest.fn(() => {
  25. return {
  26. keys: ['key0', 'key1', 'key2', 'key3', 'key4']
  27. }
  28. })
  29. expect(localStorage.key(0)).toEqual('key0')
  30. expect(localStorage.key(3)).toEqual('key3')
  31. expect(wx.getStorageInfoSync).toHaveBeenCalledTimes(2)
  32. })
  33. it('call wx.getStorageSync() with getItem()', () => {
  34. localStorage.getItem('key')
  35. expect(getStorageSync).toHaveBeenCalledTimes(1)
  36. expect(getStorageSync).toHaveBeenCalledWith('key')
  37. })
  38. it('call setStorageSync() with setItem()', () => {
  39. localStorage.setItem('key', 'value')
  40. expect(setStorageSync).toHaveBeenCalledWith('key', 'value')
  41. })
  42. it('call removeStorageSync() with removeItem()', () => {
  43. localStorage.removeItem('key')
  44. expect(removeStorageSync).toHaveBeenCalledWith('key')
  45. })
  46. it('call clearStorageInfoSync() with clear()', () => {
  47. localStorage.clear()
  48. expect(clearStorageSync).toHaveBeenCalledTimes(1)
  49. })
  50. })