|
1 |
| -import { describe, test } from 'vitest'; |
| 1 | +import { from, toArray } from 'rxjs'; |
| 2 | +import { TestScheduler } from 'rxjs/testing'; |
| 3 | +import { afterAll, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { log, logResult } from './log'; |
2 | 6 |
|
3 | 7 | describe('log', () => {
|
| 8 | + let testScheduler; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected)); |
| 12 | + }); |
| 13 | + |
| 14 | + afterAll(() => { |
| 15 | + vi.restoreAllMocks(); |
| 16 | + }); |
| 17 | + |
4 | 18 | test('default', () => {
|
5 |
| - //TODO: add test |
| 19 | + const expectedVal = { |
| 20 | + a: 'content a', |
| 21 | + b: 'content b', |
| 22 | + c: 'content c' |
| 23 | + }; |
| 24 | + |
| 25 | + const triggerVal = { |
| 26 | + a: expectedVal.a, |
| 27 | + b: expectedVal.b, |
| 28 | + c: expectedVal.c |
| 29 | + }; |
| 30 | + |
| 31 | + const expected = [ |
| 32 | + ' operators:log:default content a', |
| 33 | + ' operators:log:default content b', |
| 34 | + ' operators:log:default content c', |
| 35 | + ' operators:log:default complete!' |
| 36 | + ]; |
| 37 | + |
| 38 | + const actual = []; |
| 39 | + vi.spyOn(console, 'log').mockImplementation(v => { |
| 40 | + actual.push(stripAnsiCodes(v)); |
| 41 | + return v; |
| 42 | + }); |
| 43 | + |
| 44 | + testScheduler.run(({ cold, expectObservable, flush }) => { |
| 45 | + const stream = cold('a-b-c|', triggerVal).pipe(log('operators:log:default')); |
| 46 | + expectObservable(stream).toBe('a-b-c|', expectedVal); |
| 47 | + flush(); |
| 48 | + expect(actual).deep.equal(expected); |
| 49 | + }); |
6 | 50 | });
|
7 | 51 |
|
8 |
| - test('logResult', () => { |
9 |
| - //TODO: add test |
| 52 | + test('logResult', async () => { |
| 53 | + const actual = []; |
| 54 | + vi.spyOn(console, 'log').mockImplementation(v => { |
| 55 | + actual.push(stripAnsiCodes(v)); |
| 56 | + return v; |
| 57 | + }); |
| 58 | + |
| 59 | + const expectedVal = [ |
| 60 | + ' operators:log:result content a', |
| 61 | + ' operators:log:result content b', |
| 62 | + ' operators:log:result content c', |
| 63 | + ' operators:log:result complete!' |
| 64 | + ]; |
| 65 | + |
| 66 | + const triggerVal = ['content a', 'content b', 'content c']; |
| 67 | + |
| 68 | + await logResult('operators:log:result', from(triggerVal)); |
| 69 | + expect(actual).deep.equal(expectedVal); |
10 | 70 | });
|
11 | 71 | });
|
| 72 | + |
| 73 | +const stripAnsiCodes = str => { |
| 74 | + return str.replace( |
| 75 | + // eslint-disable-next-line security/detect-unsafe-regex, no-control-regex |
| 76 | + /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, |
| 77 | + '' |
| 78 | + ); |
| 79 | +}; |
0 commit comments