Skip to content

Commit

Permalink
Get rid of actionsEnabled: in tests & docs
Browse files Browse the repository at this point in the history
  • Loading branch information
radex committed May 7, 2021
1 parent 56a509e commit 381a1db
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 100 deletions.
34 changes: 16 additions & 18 deletions docs-master/Advanced/SharingDatabaseAcrossTargets.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,29 @@ This tells iOS to share storage directories between your targets, and in this ca
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';
import schema from './schema';
import RNFetchBlob from 'rn-fetch-blob';

const getAppGroupPath = (): string => {
let path = '';

if (Platform.OS === 'ios') {
path = `${RNFetchBlob.fs.syncPathAppGroup('group.com.example.MyAwesomeApp')}/`;
}

return path;
}

const adapter = new SQLiteAdapter({
dbName: `${getAppGroupPath()}default.db`,
schema,
});

const database = new Database({
adapter,
modelClasses: [
...
],
actionsEnabled: true,
});

export default database;
```

Expand All @@ -80,10 +79,10 @@ This tells iOS to share storage directories between your targets, and in this ca
4. Add a file named `AppGroup.swift` and paste the following:
```
import Foundation

@objc(AppGroup)
class AppGroup: NSObject {

@objc
func constantsToExport() -> [AnyHashable : Any]! {
var path = ""
Expand All @@ -92,44 +91,43 @@ This tells iOS to share storage directories between your targets, and in this ca
path = directory.path
}
}

return ["path": "\(path)/"]
}
}
```
This reads your new `Info.plist` row and exports a constant called `path` with your App Group path (shared directory path), to be used in your JS code.
5. In your JS, when creating the database, import the `path` constant from your new `AppGroup` module and prepend to your `dbName`:
```ts
import { NativeModules, Platform } from 'react-native';
import { Database } from '@nozbe/watermelondb';
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';
import schema from './schema';

const getAppGroupPath = (): string => {
let path = '';

if (Platform.OS === 'ios') {
path = NativeModules.AppGroup.path;
}

return path;
}

const adapter = new SQLiteAdapter({
dbName: `${getAppGroupPath()}default.db`,
schema,
});

const database = new Database({
adapter,
modelClasses: [
...
],
actionsEnabled: true,
});

export default database;
```

Expand Down
1 change: 0 additions & 1 deletion docs-master/Components.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ import DatabaseProvider from '@nozbe/watermelondb/DatabaseProvider'
const database = new Database({
adapter,
modelClasses: [Blog, Post, Comment],
actionsEnabled: true,
})
render(
Expand Down
1 change: 0 additions & 1 deletion docs-master/Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const database = new Database({
modelClasses: [
// Post, // ⬅️ You'll add Models to Watermelon here
],
actionsEnabled: true,
})
```

Expand Down
8 changes: 4 additions & 4 deletions src/Collection/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ describe('creating new records', () => {
expect(newModelSpy).toHaveBeenCalledTimes(1)
})
it('disallows record creating outside of an action', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()

await expectToRejectWithMessage(
tasks.create(noop),
Expand All @@ -252,7 +252,7 @@ describe('creating new records', () => {

describe('Collection observation', () => {
it('can subscribe to collection changes', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()

await database.action(() => tasks.create())

Expand Down Expand Up @@ -289,7 +289,7 @@ describe('Collection observation', () => {
expect(subscriber2).toHaveBeenCalledTimes(2)
})
it('unsubscribe can safely be called more than once', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()

const subscriber1 = jest.fn()
const unsubscribe1 = tasks.experimentalSubscribe(subscriber1)
Expand All @@ -305,7 +305,7 @@ describe('Collection observation', () => {
unsubscribe1()
})
it(`can subscribe with the same subscriber multiple times`, async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()
const trigger = () => database.action(() => tasks.create())
const subscriber = jest.fn()

Expand Down
10 changes: 5 additions & 5 deletions src/Database/CollectionMap/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Model from '../../Model'

describe('CollectionMap', () => {
it('can initialize and get models', () => {
const { db } = mockDatabase({ actionsEnabled: true })
const { db } = mockDatabase()
const map = new CollectionMap(db, [MockProject, MockTask])

expect(map.get('mock_projects').modelClass).toBe(MockProject)
Expand All @@ -13,14 +13,14 @@ describe('CollectionMap', () => {
expect(map.get('mock_tasks').table).toBe('mock_tasks')
})
it(`returns null for collections that don't exist`, () => {
const { db } = mockDatabase({ actionsEnabled: true })
const { db } = mockDatabase()
const map = new CollectionMap(db, [MockProject, MockTask])

expect(map.get('mock_comments')).toBe(null)
expect(map.get('does_not_exist')).toBe(null)
})
it(`returns null for naughty table names`, () => {
const { db } = mockDatabase({ actionsEnabled: true })
const { db } = mockDatabase()
const map = new CollectionMap(db, [MockProject, MockTask])

expect(map.get(null)).toBe(null)
Expand All @@ -30,14 +30,14 @@ describe('CollectionMap', () => {
expect(map.get('hasOwnProperty')).toBe(null)
})
it(`collection map is immutable`, () => {
const { db } = mockDatabase({ actionsEnabled: true })
const { db } = mockDatabase()
const map = new CollectionMap(db, [MockProject, MockTask])
expect(() => {
map.map.foo = 'hey'
}).toThrow()
})
it(`alerts the user of invalid model classes`, () => {
const { db } = mockDatabase({ actionsEnabled: true })
const { db } = mockDatabase()
class ModelWithMissingTable extends Model {}
expect(() => new CollectionMap(db, [ModelWithMissingTable])).toThrow(
/Model class ModelWithMissingTable passed to Database constructor is missing "static table = 'table_name'"/,
Expand Down
3 changes: 1 addition & 2 deletions src/Database/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ declare module '@nozbe/watermelondb/Database' {
options: {
adapter: DatabaseAdapter;
modelClasses: Class<Model>[];
actionsEnabled: boolean;
})

public batch(...records: (Model | null | void | false | Promise<void>)[]): Promise<void>
Expand All @@ -32,7 +31,7 @@ declare module '@nozbe/watermelondb/Database' {
): Observable<CollectionChangeSet<any> | null>

public unsafeResetDatabase(): Promise<void>

public get<T extends Model>(tableName: TableName<T>): Collection<T>
}
}
38 changes: 19 additions & 19 deletions src/Database/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import * as Q from '../QueryDescription'

describe('Database', () => {
it(`implements get()`, () => {
const { database } = mockDatabase({ actionsEnabled: true })
const { database } = mockDatabase()
expect(database.get('mock_tasks').table).toBe('mock_tasks')
expect(database.get('mock_tasks')).toBe(database.collections.get('mock_tasks'))
expect(database.get('mock_comments')).toBe(database.collections.get('mock_comments'))
})

describe('unsafeResetDatabase', () => {
it('can reset database', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()

const m1 = await database.action(() => tasks.create())
const m2 = await database.action(() => tasks.create())
Expand All @@ -29,7 +29,7 @@ describe('Database', () => {
await expectToRejectWithMessage(tasks.find(m2.id), /not found/)
})
it('throws error if reset is called from outside an Action', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()
const m1 = await database.action(() => tasks.create())

await expectToRejectWithMessage(
Expand All @@ -40,7 +40,7 @@ describe('Database', () => {
expect(await tasks.find(m1.id)).toBe(m1)
})
it('increments reset count after every reset', async () => {
const { database } = mockDatabase({ actionsEnabled: true })
const { database } = mockDatabase()
expect(database._resetCount).toBe(0)

await database.action(() => database.unsafeResetDatabase())
Expand All @@ -50,7 +50,7 @@ describe('Database', () => {
expect(database._resetCount).toBe(2)
})
it('prevents Adapter from being called during reset db', async () => {
const { database } = mockDatabase({ actionsEnabled: true })
const { database } = mockDatabase()

const checkAdapter = async () => {
expect(await database.adapter.getLocal('test')).toBe(null)
Expand All @@ -77,7 +77,7 @@ describe('Database', () => {
await checkAdapter()
})
it('Cancels Database experimental subscribers during reset', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()

// sanity check first
const subscriber1 = jest.fn()
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('Database', () => {
it.skip('Cancels Relation observation during reset', async () => {})
it.skip('Cancels Relation experimental subscribers during reset', async () => {})
it('Signals internally when database is being reset', async () => {
const { database } = mockDatabase({ actionsEnabled: true })
const { database } = mockDatabase()

expect(database._isBeingReset).toBe(false)
const promise = database.action(() => database.unsafeResetDatabase())
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Database', () => {
cloneDatabase,
tasks: tasksCollection,
comments: commentsCollection,
} = mockDatabase({ actionsEnabled: true })
} = mockDatabase()
const adapterBatchSpy = jest.spyOn(database.adapter, 'batch')

// m1, m2 will be used to test batch-updates
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('Database', () => {
expect(fetchedM4.length).toBe(0)
})
it('ignores falsy values passed', async () => {
const { database, tasks: tasksCollection } = mockDatabase({ actionsEnabled: true })
const { database, tasks: tasksCollection } = mockDatabase()
const adapterBatchSpy = jest.spyOn(database.adapter, 'batch')

const model = tasksCollection.prepareCreate()
Expand All @@ -249,7 +249,7 @@ describe('Database', () => {
expect(adapterBatchSpy).toHaveBeenLastCalledWith([['create', 'mock_tasks', model._raw]])
})
it(`can batch with an array passed as argument`, async () => {
const { database, tasks: tasksCollection } = mockDatabase({ actionsEnabled: true })
const { database, tasks: tasksCollection } = mockDatabase()
const adapterBatchSpy = jest.spyOn(database.adapter, 'batch')

const model = tasksCollection.prepareCreate()
Expand All @@ -259,7 +259,7 @@ describe('Database', () => {
expect(adapterBatchSpy).toHaveBeenLastCalledWith([['create', 'mock_tasks', model._raw]])
})
it('throws error if attempting to batch records without a pending operation', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()
const m1 = await database.action(() => tasks.create())

await expectToRejectWithMessage(
Expand All @@ -268,7 +268,7 @@ describe('Database', () => {
)
})
it('throws error if batch is called outside of an action', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()

await expectToRejectWithMessage(
database.batch(tasks.prepareCreate(noop)),
Expand All @@ -287,7 +287,7 @@ describe('Database', () => {
expect(task.name).toBe('foo1')
})
it(`throws an error if invalid arguments`, async () => {
const { database } = mockDatabase({ actionsEnabled: true })
const { database } = mockDatabase()
await expectToRejectWithMessage(
database.batch([], null),
/batch should be called with a list/,
Expand All @@ -297,7 +297,7 @@ describe('Database', () => {

describe('Observation', () => {
it('implements withChangesForTables', async () => {
const { database, projects, tasks, comments } = mockDatabase({ actionsEnabled: true })
const { database, projects, tasks, comments } = mockDatabase()

const observer = jest.fn()
database.withChangesForTables(['mock_projects', 'mock_tasks']).subscribe(observer)
Expand Down Expand Up @@ -339,7 +339,7 @@ describe('Database', () => {
])
})
it('can subscribe to change signals for particular tables', async () => {
const { database, projects, tasks, comments } = mockDatabase({ actionsEnabled: true })
const { database, projects, tasks, comments } = mockDatabase()

const subscriber1 = jest.fn()
const unsubscribe1 = database.experimentalSubscribe([], subscriber1)
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('Database', () => {
unsubscribe3()
})
it('unsubscribe can safely be called more than once', async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()

const subscriber1 = jest.fn()
const unsubscribe1 = database.experimentalSubscribe(['mock_tasks'], subscriber1)
Expand All @@ -405,7 +405,7 @@ describe('Database', () => {
unsubscribe1()
})
it(`can subscribe with the same subscriber multiple times`, async () => {
const { database, tasks } = mockDatabase({ actionsEnabled: true })
const { database, tasks } = mockDatabase()

const subscriber = jest.fn()
const unsubscribe1 = database.experimentalSubscribe(['mock_tasks'], subscriber)
Expand All @@ -426,7 +426,7 @@ describe('Database', () => {
expect(subscriber).toHaveBeenCalledTimes(4)
})
it('has new objects cached before calling subscribers (regression test)', async () => {
const { database, projects, tasks } = mockDatabase({ actionsEnabled: true })
const { database, projects, tasks } = mockDatabase()

const project = projects.prepareCreate()
const task = tasks.prepareCreate(t => {
Expand Down Expand Up @@ -604,7 +604,7 @@ describe('Database', () => {
expect(called2).toBe(0)
})
it('aborts all pending actions if database is reset', async () => {
const { database } = mockDatabase({ actionsEnabled: true })
const { database } = mockDatabase()

let promise1
let promise2
Expand Down
2 changes: 1 addition & 1 deletion src/DatabaseProvider/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function MockComponent() {
describe('DatabaseProvider', () => {
let database
beforeAll(() => {
database = mockDatabase({ actionsEnabled: true }).db
database = mockDatabase().db
})
it('throws if no database or adapter supplied', () => {
expect(() => {
Expand Down
Loading

0 comments on commit 381a1db

Please sign in to comment.