Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 18 additions & 90 deletions test/unit/bug-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,23 @@
* - 'npm run test:browser' so it runs in the browser
*/
import assert from 'assert';
import AsyncTestUtil from 'async-test-util';
import config from './config.ts';

import {
createRxDatabase,
randomToken
randomToken,
addRxPlugin
} from '../../plugins/core/index.mjs';
import {
isNode
} from '../../plugins/test-utils/index.mjs';
RxDBCleanupPlugin
} from '../../plugins/cleanup/index.mjs';
addRxPlugin(RxDBCleanupPlugin);

describe('bug-report.test.js', () => {
it('should fail because it reproduces the bug', async function () {
// insert -> remove -> cleanup -> insert the same primary key again:
// the second insert succeeds but queries do not see the document.

/**
* If your test should only run in nodejs or only run in the browser,
* you should comment in the return operator and adapt the if statement.
*/
if (
!isNode // runs only in node
// isNode // runs only in the browser
) {
// return;
}

if (!config.storage.hasMultiInstance) {
return;
}

// create a schema
const mySchema = {
version: 0,
primaryKey: 'passportId',
Expand All @@ -46,96 +34,36 @@ describe('bug-report.test.js', () => {
passportId: {
type: 'string',
maxLength: 100
},
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
age: {
type: 'integer',
minimum: 0,
maximum: 150
}
}
};

/**
* Always generate a random database-name
* to ensure that different test runs do not affect each other.
*/
const name = randomToken(10);

// create a database
const db = await createRxDatabase({
name,
/**
* By calling config.storage.getStorage(),
* we can ensure that all variations of RxStorage are tested in the CI.
*/
storage: config.storage.getStorage(),
eventReduce: true,
ignoreDuplicate: true
});
// create a collection
const collections = await db.addCollections({
mycollection: {
schema: mySchema
}
});

// insert a document
await collections.mycollection.insert({
passportId: 'foobar',
firstName: 'Bob',
lastName: 'Kelso',
age: 56
});

/**
* to simulate the event-propagation over multiple browser-tabs,
* we create the same database again
*/
const dbInOtherTab = await createRxDatabase({
name,
storage: config.storage.getStorage(),
eventReduce: true,
ignoreDuplicate: true
});
// create a collection
const collectionInOtherTab = await dbInOtherTab.addCollections({
mycollection: {
schema: mySchema
}
});

// find the document in the other tab
const myDocument = await collectionInOtherTab.mycollection
.findOne()
.where('firstName')
.eq('Bob')
.exec();

/*
* assert things,
* here your tests should fail to show that there is a bug
*/
assert.strictEqual(myDocument.age, 56);
// first lifecycle of the document
await collections.mycollection.insert({ passportId: 'foobar' });
const doc = await collections.mycollection.findOne('foobar').exec(true);
await doc.remove();
await collections.mycollection.cleanup(0);

// insert the same primary key again
await collections.mycollection.insert({ passportId: 'foobar' });

// you can also wait for events
const emitted: any[] = [];
const sub = collectionInOtherTab.mycollection
.findOne().$
.subscribe(doc => {
emitted.push(doc);
});
await AsyncTestUtil.waitUntil(() => emitted.length === 1);
// the document was just inserted, so it must be found
const found = await collections.mycollection.findOne('foobar').exec();
assert.ok(found, 'findOne() must return the document that was just inserted');

// clean up afterwards
sub.unsubscribe();
db.close();
dbInOtherTab.close();
});
});
Loading