|
| 1 | +const MongoDBEngine = require('../').default; |
| 2 | +const { MongoDBDatabaseEngine } = require('../'); |
| 3 | + |
| 4 | +jest.setTimeout(10 * 1000); |
| 5 | + |
| 6 | +jest.mock('perf_hooks'); |
| 7 | + |
| 8 | +const baseVersion = '0'; |
| 9 | +const getAdvisoryLockId = (databaseName, ...names) => { |
| 10 | + return [String(databaseName.length), String(names.join().length)]; |
| 11 | +}; |
| 12 | +const getUserInfo = () => Promise.resolve(`Jest`); |
| 13 | + |
| 14 | +const databaseName = 'synor'; |
| 15 | +const collectionName = 'test_record'; |
| 16 | +const params = `synor_migration_record_collection=${collectionName}`; |
| 17 | +const uri = `mongodb://mongo:mongo@127.0.0.1:27017/${databaseName}?${params}`; |
| 18 | + |
| 19 | +describe('module exports', () => { |
| 20 | + test('default export exists', () => { |
| 21 | + expect(typeof MongoDBEngine).toBe('function'); |
| 22 | + }); |
| 23 | + |
| 24 | + test('named export exists', () => { |
| 25 | + expect(typeof MongoDBDatabaseEngine).toBe('function'); |
| 26 | + }); |
| 27 | + |
| 28 | + test('default and named exports are same', () => { |
| 29 | + expect(MongoDBEngine).toBe(MongoDBDatabaseEngine); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('initialization', () => { |
| 34 | + let dbUri; |
| 35 | + const helpers = { |
| 36 | + baseVersion, |
| 37 | + getAdvisoryLockId, |
| 38 | + getUserInfo |
| 39 | + }; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + dbUri = uri; |
| 43 | + helpers.baseVersion = baseVersion; |
| 44 | + helpers.getAdvisoryLockId = getAdvisoryLockId; |
| 45 | + helpers.getUserInfo = getUserInfo; |
| 46 | + }); |
| 47 | + |
| 48 | + test.each([undefined, null, 0])('throws if uri is %s', uri => { |
| 49 | + expect(() => MongoDBDatabaseEngine(uri, helpers)).toThrow(); |
| 50 | + }); |
| 51 | + |
| 52 | + test('throws if uri is empty', () => { |
| 53 | + dbUri = ' '; |
| 54 | + expect(() => MongoDBDatabaseEngine(dbUri, helpers)).toThrow(); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('helpers validation', () => { |
| 58 | + beforeEach(() => { |
| 59 | + helpers.getAdvisoryLockId = getAdvisoryLockId; |
| 60 | + helpers.getUserInfo = getUserInfo; |
| 61 | + }); |
| 62 | + |
| 63 | + test(`throws if getAdvisoryLockId is missing`, () => { |
| 64 | + delete helpers.getAdvisoryLockId; |
| 65 | + expect(() => MongoDBDatabaseEngine(dbUri, helpers)).toThrow(); |
| 66 | + }); |
| 67 | + |
| 68 | + test(`throws if getAdvisoryLockId is not function`, () => { |
| 69 | + helpers.getAdvisoryLockId = ''; |
| 70 | + expect(() => MongoDBDatabaseEngine(dbUri, helpers)).toThrow(); |
| 71 | + helpers.getAdvisoryLockId = null; |
| 72 | + expect(() => MongoDBDatabaseEngine(dbUri, helpers)).toThrow(); |
| 73 | + }); |
| 74 | + |
| 75 | + test(`throws if getUserInfo is missing`, () => { |
| 76 | + delete helpers.getUserInfo; |
| 77 | + expect(() => MongoDBDatabaseEngine(dbUri, helpers)).toThrow(); |
| 78 | + }); |
| 79 | + |
| 80 | + test(`throws if getUserInfo is not function`, () => { |
| 81 | + helpers.getUserInfo = ''; |
| 82 | + expect(() => MongoDBDatabaseEngine(dbUri, helpers)).toThrow(); |
| 83 | + helpers.getUserInfo = null; |
| 84 | + expect(() => MongoDBDatabaseEngine(dbUri, helpers)).toThrow(); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments