|
1 | 1 | /**
|
2 | 2 | * @format
|
3 |
| - * @lint-ignore-every XPLATJSCOPYRIGHT1 |
4 | 3 | */
|
| 4 | +/* eslint-disable no-shadow */ |
5 | 5 |
|
6 | 6 | import 'react-native';
|
7 |
| -import React from 'react'; |
8 |
| -import App from '../App'; |
9 | 7 |
|
10 |
| -// Note: test renderer must be required after react-native. |
11 |
| -import renderer from 'react-test-renderer'; |
| 8 | +import AsyncStorage from '@react-native-community/async-storage'; |
12 | 9 |
|
13 |
| -it('renders correctly', () => { |
14 |
| - renderer.create(<App />); |
| 10 | +describe('Async Storage mock functionality', () => { |
| 11 | + describe('Promise based', () => { |
| 12 | + it('can read/write data to/from storage', async () => { |
| 13 | + const newData = Math.floor(Math.random() * 1000); |
| 14 | + |
| 15 | + await AsyncStorage.setItem('key', newData); |
| 16 | + |
| 17 | + const data = await AsyncStorage.getItem('key'); |
| 18 | + |
| 19 | + expect(data).toBe(newData); |
| 20 | + }); |
| 21 | + |
| 22 | + it('can clear storage', async () => { |
| 23 | + await AsyncStorage.setItem('temp_key', Math.random() * 1000); |
| 24 | + |
| 25 | + let currentValue = await AsyncStorage.getItem('temp_key'); |
| 26 | + |
| 27 | + expect(currentValue).not.toBeNull(); |
| 28 | + |
| 29 | + await AsyncStorage.clear(); |
| 30 | + |
| 31 | + currentValue = await AsyncStorage.getItem('temp_key'); |
| 32 | + |
| 33 | + expect(currentValue).toBeNull(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('can clear entries in storage', async () => { |
| 37 | + await AsyncStorage.setItem('random1', Math.random() * 1000); |
| 38 | + await AsyncStorage.setItem('random2', Math.random() * 1000); |
| 39 | + |
| 40 | + let data1 = await AsyncStorage.getItem('random1'); |
| 41 | + let data2 = await AsyncStorage.getItem('random2'); |
| 42 | + |
| 43 | + expect(data1).not.toBeNull(); |
| 44 | + expect(data2).not.toBeNull(); |
| 45 | + |
| 46 | + await AsyncStorage.removeItem('random1'); |
| 47 | + await AsyncStorage.removeItem('random2'); |
| 48 | + data1 = await AsyncStorage.getItem('random1'); |
| 49 | + data2 = await AsyncStorage.getItem('random2'); |
| 50 | + expect(data2).toBeNull(); |
| 51 | + expect(data1).toBeNull(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('can use merge with current data in storage', async () => { |
| 55 | + let originalPerson = { |
| 56 | + name: 'Jerry', |
| 57 | + age: 21, |
| 58 | + characteristics: { |
| 59 | + hair: 'black', |
| 60 | + eyes: 'green', |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + await AsyncStorage.setItem('person', JSON.stringify(originalPerson)); |
| 65 | + |
| 66 | + originalPerson.name = 'Harry'; |
| 67 | + originalPerson.characteristics.hair = 'red'; |
| 68 | + originalPerson.characteristics.shoeSize = 40; |
| 69 | + |
| 70 | + await AsyncStorage.mergeItem('person', JSON.stringify(originalPerson)); |
| 71 | + |
| 72 | + const currentPerson = await AsyncStorage.getItem('person'); |
| 73 | + const person = JSON.parse(currentPerson); |
| 74 | + |
| 75 | + expect(person).toHaveProperty('name', 'Harry'); |
| 76 | + expect(person.characteristics).toHaveProperty('hair', 'red'); |
| 77 | + expect(person.characteristics).toHaveProperty('shoeSize', 40); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('Callback based', () => { |
| 82 | + it('can read/write data to/from storage', done => { |
| 83 | + const newData = Math.floor(Math.random() * 1000); |
| 84 | + |
| 85 | + AsyncStorage.setItem('key', newData, function() { |
| 86 | + AsyncStorage.getItem('key', function(_, value) { |
| 87 | + expect(value).toBe(newData); |
| 88 | + done(); |
| 89 | + }).catch(e => done.fail(e)); |
| 90 | + }); |
| 91 | + }); |
| 92 | + it('can clear storage', done => { |
| 93 | + AsyncStorage.setItem('temp_key', Math.random() * 1000, () => { |
| 94 | + AsyncStorage.getItem('temp_key', (_, currentValue) => { |
| 95 | + expect(currentValue).not.toBeNull(); |
| 96 | + AsyncStorage.clear(() => { |
| 97 | + AsyncStorage.getItem('temp_key', (_, value) => { |
| 98 | + expect(value).toBeNull(); |
| 99 | + done(); |
| 100 | + }).catch(e => done.fail(e)); |
| 101 | + }); |
| 102 | + }).catch(e => done.fail(e)); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('can clear entries in storage', done => { |
| 107 | + AsyncStorage.setItem('random1', Math.random() * 1000, () => { |
| 108 | + AsyncStorage.setItem('random2', Math.random() * 1000, () => { |
| 109 | + AsyncStorage.getItem('random1', (_, data1) => { |
| 110 | + AsyncStorage.getItem('random2', (_, data2) => { |
| 111 | + expect(data1).not.toBeNull(); |
| 112 | + expect(data2).not.toBeNull(); |
| 113 | + |
| 114 | + AsyncStorage.removeItem('random1', () => { |
| 115 | + AsyncStorage.removeItem('random2', () => { |
| 116 | + AsyncStorage.getItem('random1', (_, value1) => { |
| 117 | + AsyncStorage.getItem('random2', (_, value2) => { |
| 118 | + expect(value1).toBeNull(); |
| 119 | + expect(value2).toBeNull(); |
| 120 | + done(); |
| 121 | + }).catch(e => done.fail(e)); |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | + }).catch(e => done.fail(e)); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('can use merge with current data in storage', done => { |
| 132 | + let originalPerson = { |
| 133 | + name: 'Jerry', |
| 134 | + age: 21, |
| 135 | + characteristics: { |
| 136 | + hair: 'black', |
| 137 | + eyes: 'green', |
| 138 | + }, |
| 139 | + }; |
| 140 | + |
| 141 | + AsyncStorage.setItem('person', JSON.stringify(originalPerson), () => { |
| 142 | + originalPerson.name = 'Harry'; |
| 143 | + originalPerson.characteristics.hair = 'red'; |
| 144 | + originalPerson.characteristics.shoeSize = 40; |
| 145 | + AsyncStorage.mergeItem('person', JSON.stringify(originalPerson), () => { |
| 146 | + AsyncStorage.getItem('person', (_, currentPerson) => { |
| 147 | + const person = JSON.parse(currentPerson); |
| 148 | + expect(person).toHaveProperty('name', 'Harry'); |
| 149 | + expect(person.characteristics).toHaveProperty('hair', 'red'); |
| 150 | + expect(person.characteristics).toHaveProperty('shoeSize', 40); |
| 151 | + done(); |
| 152 | + }).catch(e => done.fail(e)); |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
15 | 157 | });
|
0 commit comments