|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +import { ConnectionProvider, newError } from '../src' |
| 20 | +import Driver from '../src/driver' |
| 21 | +import { Logger } from '../src/internal/logger' |
| 22 | +import { ConfiguredCustomResolver } from '../src/internal/resolver' |
| 23 | + |
| 24 | +describe('Driver', () => { |
| 25 | + let driver: Driver | null |
| 26 | + let connectionProvider: ConnectionProvider |
| 27 | + const META_INFO = { |
| 28 | + routing: false, |
| 29 | + typename: '', |
| 30 | + address: 'localhost' |
| 31 | + } |
| 32 | + const CONFIG = {} |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + connectionProvider = new ConnectionProvider() |
| 36 | + connectionProvider.close = jest.fn(() => Promise.resolve()) |
| 37 | + driver = new Driver( |
| 38 | + META_INFO, |
| 39 | + CONFIG, |
| 40 | + mockCreateConnectonProvider(connectionProvider) |
| 41 | + ) |
| 42 | + }) |
| 43 | + |
| 44 | + afterEach(async () => { |
| 45 | + if (driver) { |
| 46 | + await driver.close() |
| 47 | + driver = null |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + it.each([ |
| 52 | + ['Promise.resolve(true)', Promise.resolve(true)], |
| 53 | + ['Promise.resolve(false)', Promise.resolve(false)], |
| 54 | + [ |
| 55 | + "Promise.reject(newError('something went wrong'))", |
| 56 | + Promise.reject(newError('something went wrong')) |
| 57 | + ] |
| 58 | + ])('.supportsMultiDb() => %s', (_, expectedPromise) => { |
| 59 | + connectionProvider.supportsMultiDb = jest.fn(() => expectedPromise) |
| 60 | + |
| 61 | + const promise: Promise<boolean> = driver!!.supportsMultiDb() |
| 62 | + |
| 63 | + expect(promise).toBe(expectedPromise) |
| 64 | + }) |
| 65 | + |
| 66 | + it.each([ |
| 67 | + ['Promise.resolve(true)', Promise.resolve(true)], |
| 68 | + ['Promise.resolve(false)', Promise.resolve(false)], |
| 69 | + [ |
| 70 | + "Promise.reject(newError('something went wrong'))", |
| 71 | + Promise.reject(newError('something went wrong')) |
| 72 | + ] |
| 73 | + ])('.supportsTransactionConfig() => %s', (_, expectedPromise) => { |
| 74 | + connectionProvider.supportsTransactionConfig = jest.fn( |
| 75 | + () => expectedPromise |
| 76 | + ) |
| 77 | + |
| 78 | + const promise: Promise<boolean> = driver!!.supportsTransactionConfig() |
| 79 | + |
| 80 | + expect(promise).toBe(expectedPromise) |
| 81 | + }) |
| 82 | + |
| 83 | + function mockCreateConnectonProvider(connectionProvider: ConnectionProvider) { |
| 84 | + return ( |
| 85 | + id: number, |
| 86 | + config: Object, |
| 87 | + log: Logger, |
| 88 | + hostNameResolver: ConfiguredCustomResolver |
| 89 | + ) => connectionProvider |
| 90 | + } |
| 91 | +}) |
0 commit comments