|
1 | | -// import { mount } from '@src/helpers/test' |
2 | | -// import { useLocalStorage } from '@src/vue-use-kit' |
| 1 | +import { mount } from '@src/helpers/test' |
| 2 | +import { useLocalStorage } from '@src/vue-use-kit' |
| 3 | + |
| 4 | +const localStorageMock = () => { |
| 5 | + let store = {} as any |
| 6 | + return { |
| 7 | + getItem(key: any) { |
| 8 | + return store[key] |
| 9 | + }, |
| 10 | + setItem(key: any, value: any) { |
| 11 | + store[key] = String(value) |
| 12 | + }, |
| 13 | + clear() { |
| 14 | + store = {} |
| 15 | + }, |
| 16 | + removeItem(key: any) { |
| 17 | + delete store[key] |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + ;(window as any).localStorage = localStorageMock() |
| 24 | +}) |
3 | 25 |
|
4 | 26 | afterEach(() => { |
5 | 27 | jest.clearAllMocks() |
6 | 28 | }) |
7 | 29 |
|
8 | | -describe('useLocalStorage', () => { |
9 | | - it('should do something', () => { |
10 | | - // Add test here |
| 30 | +const testComponent = ( |
| 31 | + key = 'key', |
| 32 | + itemValue: any = '', |
| 33 | + opts = { isParsing: false } as any, |
| 34 | + onMount = true |
| 35 | +) => ({ |
| 36 | + template: ` |
| 37 | + <div> |
| 38 | + <div id="item">{{ item }}</div> |
| 39 | + <div id="itemJson"> |
| 40 | + {{ item && item.value1 }} - {{ item && item.value2 }} |
| 41 | + </div> |
| 42 | + <button id="getItem" @click="getItem"></button> |
| 43 | + <button id="setItem" @click="setItem(itemValue)"></button> |
| 44 | + <button id="removeItem" @click="removeItem"></button> |
| 45 | + </div> |
| 46 | + `, |
| 47 | + setup() { |
| 48 | + const { item, getItem, setItem, removeItem } = useLocalStorage( |
| 49 | + key, |
| 50 | + opts, |
| 51 | + onMount |
| 52 | + ) |
| 53 | + |
| 54 | + return { |
| 55 | + item, |
| 56 | + getItem, |
| 57 | + setItem, |
| 58 | + removeItem, |
| 59 | + itemValue |
| 60 | + } |
| 61 | + } |
| 62 | +}) |
| 63 | + |
| 64 | +describe('useItem', () => { |
| 65 | + it('should get a item with the given value', async () => { |
| 66 | + const itemKey = 'itemKey' |
| 67 | + const itemValue = 'itemValue' |
| 68 | + ;(window as any).localStorage.setItem(itemKey, itemValue) |
| 69 | + const wrapper = mount(testComponent(itemKey)) |
| 70 | + await wrapper.vm.$nextTick() |
| 71 | + expect(wrapper.html()).toContain(itemValue) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should set a item with the given value', async () => { |
| 75 | + const itemKey = 'itemKey' |
| 76 | + const itemValue = 'itemValue' |
| 77 | + const wrapper = mount(testComponent(itemKey, itemValue)) |
| 78 | + await wrapper.vm.$nextTick() |
| 79 | + expect(localStorage.getItem(itemKey)).toContain(itemValue) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should remove a item with the given value', async () => { |
| 83 | + const itemKey = 'itemKey' |
| 84 | + const itemValue = 'itemValue' |
| 85 | + const wrapper = mount(testComponent(itemKey, itemValue)) |
| 86 | + await wrapper.vm.$nextTick() |
| 87 | + expect(localStorage.getItem(itemKey)).toContain(itemValue) |
| 88 | + wrapper.find('#removeItem').trigger('click') |
| 89 | + await wrapper.vm.$nextTick() |
| 90 | + expect(localStorage.getItem(itemKey)).toBeFalsy() |
| 91 | + }) |
| 92 | + |
| 93 | + it('should correctly get and set the isParsing object', async () => { |
| 94 | + const itemKey = 'itemKey' |
| 95 | + const itemValue = { value1: 'testValue1', value2: 'testValue2' } |
| 96 | + const wrapper = mount( |
| 97 | + testComponent(itemKey, itemValue, { isParsing: true }) |
| 98 | + ) |
| 99 | + wrapper.find('#setItem').trigger('click') |
| 100 | + await wrapper.vm.$nextTick() |
| 101 | + expect(wrapper.find('#itemJson').html()).toContain(itemValue.value1) |
| 102 | + expect(wrapper.find('#itemJson').html()).toContain(itemValue.value2) |
| 103 | + |
| 104 | + // Also check that when clicking #getItem we still get a proper JSON object |
| 105 | + wrapper.find('#getItem').trigger('click') |
| 106 | + await wrapper.vm.$nextTick() |
| 107 | + expect(wrapper.find('#itemJson').html()).toContain(itemValue.value1) |
| 108 | + expect(wrapper.find('#itemJson').html()).toContain(itemValue.value2) |
| 109 | + }) |
| 110 | + |
| 111 | + it('should correctly get using the deserializer', async () => { |
| 112 | + const itemKey = 'itemKey' |
| 113 | + const itemValue = { value1: 'testValue1', value2: 'testValue2' } |
| 114 | + const deserializerVal = { value1: 'gatto', value2: 'topo' } |
| 115 | + const wrapper = mount( |
| 116 | + testComponent(itemKey, itemValue, { |
| 117 | + isParsing: true, |
| 118 | + deserializer: () => deserializerVal |
| 119 | + }) |
| 120 | + ) |
| 121 | + await wrapper.vm.$nextTick() |
| 122 | + expect(wrapper.find('#itemJson').html()).toContain(deserializerVal.value1) |
| 123 | + expect(wrapper.find('#itemJson').html()).toContain(deserializerVal.value2) |
| 124 | + }) |
| 125 | + |
| 126 | + it('should ignore deserializer when isParsing is false', async () => { |
| 127 | + const itemKey = 'itemKey' |
| 128 | + const itemValue = { value1: 'testValue1', value2: 'testValue2' } |
| 129 | + const deserializerVal = { value1: 'gatto', value2: 'topo' } |
| 130 | + const wrapper = mount( |
| 131 | + testComponent(itemKey, itemValue, { |
| 132 | + isParsing: false, |
| 133 | + deserializer: () => deserializerVal |
| 134 | + }) |
| 135 | + ) |
| 136 | + await wrapper.vm.$nextTick() |
| 137 | + expect(wrapper.find('#item').html()).toContain(itemValue.value1) |
| 138 | + expect(wrapper.find('#item').html()).toContain(itemValue.value2) |
| 139 | + expect(wrapper.find('#itemJson').html()).not.toContain( |
| 140 | + deserializerVal.value1 |
| 141 | + ) |
| 142 | + expect(wrapper.find('#itemJson').html()).not.toContain( |
| 143 | + deserializerVal.value2 |
| 144 | + ) |
| 145 | + }) |
| 146 | + |
| 147 | + it('should correctly set using the serializer', async () => { |
| 148 | + const itemKey = 'itemKey' |
| 149 | + const itemValue = { value1: 'testValue1', value2: 'testValue2' } |
| 150 | + const serializerVal = { value1: 'testValue1+1', value2: 'testValue2+1' } |
| 151 | + const wrapper = mount( |
| 152 | + testComponent(itemKey, itemValue, { |
| 153 | + isParsing: true, |
| 154 | + serializer: (obj: any) => |
| 155 | + JSON.stringify({ |
| 156 | + value1: `${obj.value1}+1`, |
| 157 | + value2: `${obj.value2}+1` |
| 158 | + }) |
| 159 | + }) |
| 160 | + ) |
| 161 | + wrapper.find('#setItem').trigger('click') |
| 162 | + wrapper.find('#getItem').trigger('click') |
| 163 | + await wrapper.vm.$nextTick() |
| 164 | + expect(wrapper.find('#itemJson').html()).toContain(serializerVal.value1) |
| 165 | + expect(wrapper.find('#itemJson').html()).toContain(serializerVal.value2) |
| 166 | + }) |
| 167 | + |
| 168 | + it('should ignore serializer when isParsing is false', async () => { |
| 169 | + const itemKey = 'itemKey' |
| 170 | + const itemValue = { value1: 'testValue1', value2: 'testValue2' } |
| 171 | + const serializerVal = { value1: 'testValue1+1', value2: 'testValue2+1' } |
| 172 | + const wrapper = mount( |
| 173 | + testComponent(itemKey, itemValue, { |
| 174 | + isParsing: false, |
| 175 | + serializer: (obj: any) => |
| 176 | + JSON.stringify({ |
| 177 | + value1: `${obj.value1}+1`, |
| 178 | + value2: `${obj.value2}+1` |
| 179 | + }) |
| 180 | + }) |
| 181 | + ) |
| 182 | + wrapper.find('#setItem').trigger('click') |
| 183 | + wrapper.find('#getItem').trigger('click') |
| 184 | + await wrapper.vm.$nextTick() |
| 185 | + expect(wrapper.find('#item').html()).toContain('[object Object]') |
| 186 | + expect(wrapper.find('#itemJson').html()).not.toContain(serializerVal.value1) |
| 187 | + expect(wrapper.find('#itemJson').html()).not.toContain(serializerVal.value2) |
11 | 188 | }) |
12 | 189 | }) |
0 commit comments