rn-async-database
Version:
A simple database base on react-native AsyncStorage.
87 lines (78 loc) • 2.85 kB
JavaScript
;
jest.dontMock('../model');
jest.dontMock('../store');
var astore = require.requireActual('./mockStorage.js');
jest.setMock('@react-native-async-storage/async-storage', { AsyncStorage: astore });
describe('store Tests', function () {
var Store;
beforeEach(function () {
var Store_ = require('../store');
Store = new Store_({ dbName: 'rn-async-database' });
});
afterEach(function () {
astore._forceClear();
});
pit('should create model', function () {
var model = Store.model('newModel');
return model.add({ foo: 'bar' }).then(function () {
return astore.getAllKeys();
}).then(function (keys) {
return expect(keys).toEqual(['rn-async-database']);
});
});
pit('should clear only rn-async-database created keys', function () {
astore.setItem('SomeOtherLibrary', 'Foobar');
var model = Store.model('newModel');
return model.add({ foo: 'bar' }).then(function () {
return astore.getAllKeys();
}).then(function (keys) {
return expect(keys).toEqual(['SomeOtherLibrary', 'rn-async-database']);
}).then(function () {
return Store.clear();
}).then(astore.getAllKeys).then(function (keys) {
return expect(keys).toEqual(['SomeOtherLibrary']);
});
});
pit('should run migrations', function () {
var migrations = require('../migrations');
migrations.push({
version: 0.2,
perform: jest.genMockFunction()
});
return Store.migrate().then(function () {
expect(migrations[0].perform).toBeCalled();
});
});
pit('should run partial migrations', function () {
var migrations = require('../migrations');
migrations.push({
version: 0.2,
perform: jest.genMockFunction()
});
migrations.push({
version: 0.3,
perform: jest.genMockFunction()
});
astore.setItem('rn-async-database_version', '0.2');
return Store.migrate().then(function () {
expect(migrations[0].perform).not.toBeCalled();
expect(migrations[1].perform).toBeCalled();
});
});
pit('should not run migrations', function () {
var migrations = require('../migrations');
migrations.push({
version: 0.2,
perform: jest.genMockFunction()
});
migrations.push({
version: 0.3,
perform: jest.genMockFunction()
});
astore.setItem('rn-async-database_version', '0.3');
return Store.migrate().then(function () {
expect(migrations[0].perform).not.toBeCalled();
expect(migrations[1].perform).not.toBeCalled();
});
});
});