|
| 1 | +import path from 'path' |
| 2 | +import {FileSource, AbstractSource} from '../../src/source' |
| 3 | +import EventEmitter from 'events' |
| 4 | +import {ASYNC, FAILURE, INITIAL, READY, SYNC} from '../../src/source/abstract' |
| 5 | + |
| 6 | +const FOOBAR = path.resolve(__dirname, '../stub/foobar.json') |
| 7 | + |
| 8 | +describe('source/file', () => { |
| 9 | + describe('constructor', () => { |
| 10 | + it('returns proper instance', () => { |
| 11 | + const target = 'foo' |
| 12 | + const emitter = new EventEmitter() |
| 13 | + const api = { encoding: 'utf' } |
| 14 | + const opts = {emitter, target, api} |
| 15 | + const source = new FileSource(opts) |
| 16 | + |
| 17 | + expect(source).toBeInstanceOf(AbstractSource) |
| 18 | + expect(source.status).toBe(INITIAL) |
| 19 | + expect(source.type).toBe('file') |
| 20 | + expect(source.id).toEqual(expect.any(String)) |
| 21 | + expect(source.target).toBe(target) |
| 22 | + expect(source.opts).toBe(opts) |
| 23 | + expect(source.emitter).toBe(emitter) |
| 24 | + }) |
| 25 | + }) |
| 26 | + |
| 27 | + describe('proto', () => { |
| 28 | + describe('inherited from AbstractSource', () => { |
| 29 | + const cases = ['open', 'emit', 'on', 'setStatus'] |
| 30 | + |
| 31 | + cases.forEach(method => { |
| 32 | + it(method, () => { |
| 33 | + expect(FileSource.prototype[method]).toBe(AbstractSource.prototype[method]) |
| 34 | + }) |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + const emitter = new EventEmitter() |
| 39 | + const target = FOOBAR |
| 40 | + |
| 41 | + describe('connect', () => { |
| 42 | + it('fetches file synchronously', () => { |
| 43 | + const source = new FileSource({emitter, target, mode: SYNC}) |
| 44 | + |
| 45 | + expect(source.connect()).toBe(source) |
| 46 | + expect(source.data).toEqual({foo: 'bar'}) |
| 47 | + expect(source.status).toBe(READY) |
| 48 | + }) |
| 49 | + |
| 50 | + it('fetches file asynchronously', done => { |
| 51 | + const source = new FileSource({emitter, target, mode: ASYNC}) |
| 52 | + expect(source.connect()).toBe(source) |
| 53 | + |
| 54 | + source.on(READY, () => { |
| 55 | + expect(source.data).toEqual({foo: 'bar'}) |
| 56 | + expect(source.status).toBe(READY) |
| 57 | + done() |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('switches status to failed on any error', () => { |
| 62 | + const source = new FileSource({emitter, target: null, mode: SYNC}) |
| 63 | + source.connect() |
| 64 | + |
| 65 | + expect(source.status).toBe(FAILURE) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + describe('getters', () => { |
| 70 | + const source = new FileSource({emitter, target, mode: SYNC}) |
| 71 | + source.data = {foo: 'bar'} |
| 72 | + |
| 73 | + describe('has', () => { |
| 74 | + it('asserts status', () => { |
| 75 | + source.status = INITIAL |
| 76 | + expect(() => source.has('foo')).toThrow('Invalid source status: initial') |
| 77 | + }) |
| 78 | + |
| 79 | + it('returns value if path exists', () => { |
| 80 | + source.status = READY |
| 81 | + expect(source.has('foo')).toBeTruthy() |
| 82 | + }) |
| 83 | + |
| 84 | + it('returns false otherwise', () => { |
| 85 | + source.status = READY |
| 86 | + expect(source.has('baz')).toBeFalsy() |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + describe('get', () => { |
| 91 | + it('asserts status', () => { |
| 92 | + source.status = FAILURE |
| 93 | + expect(() => source.get('foo')).toThrow('Invalid source status: failure') |
| 94 | + }) |
| 95 | + |
| 96 | + it('returns value if path exists', () => { |
| 97 | + source.status = READY |
| 98 | + expect(source.get('foo')).toBe('bar') |
| 99 | + }) |
| 100 | + |
| 101 | + it('returns undefined ithrwise', () => { |
| 102 | + source.status = READY |
| 103 | + expect(source.get('baz')).toBeUndefined() |
| 104 | + }) |
| 105 | + }) |
| 106 | + }) |
| 107 | + }) |
| 108 | +}) |
0 commit comments