Skip to content

Commit 9a69f78

Browse files
committed
Add some basic tests
1 parent c870bd6 commit 9a69f78

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

packages/logger/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dev": "gulp dev",
1111
"test": "run-p test:browser test:node",
1212
"test:browser": "karma start --single-run",
13+
"test:browser:debug": "karma start --browsers Chrome --auto-watch",
1314
"test:node": "nyc --reporter lcovonly -- mocha test/**/*.test.* --compilers ts:ts-node/register --exit",
1415
"prepare": "gulp build"
1516
},

packages/logger/test/logger.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,80 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
import { expect } from "chai";
18+
import { spy as Spy } from "sinon";
19+
import { Logger, LogLevel } from "../src/logger";
20+
import { setLogLevel } from "../index";
21+
import { debug } from "util";
22+
23+
describe('@firebase/logger', () => {
24+
const message = 'Hello there!';
25+
let client: Logger;
26+
const spies = {
27+
debugSpy: null,
28+
logSpy: null,
29+
infoSpy: null,
30+
warnSpy: null,
31+
errorSpy: null
32+
}
33+
/**
34+
* Before each test, instantiate a new instance of Logger and set the log
35+
* level so that all
36+
*
37+
* Also spy on all of the console methods so we can assert against them as
38+
* needed
39+
*/
40+
beforeEach(() => {
41+
client = new Logger();
42+
43+
spies.debugSpy = Spy(console, 'debug');
44+
spies.logSpy = Spy(console, 'log');
45+
spies.infoSpy = Spy(console, 'info');
46+
spies.warnSpy = Spy(console, 'warn');
47+
spies.errorSpy = Spy(console, 'error');
48+
});
49+
50+
afterEach(() => {
51+
spies.debugSpy.restore();
52+
spies.logSpy.restore();
53+
spies.infoSpy.restore();
54+
spies.warnSpy.restore();
55+
spies.errorSpy.restore();
56+
});
57+
58+
function testLog(message, channel, shouldLog) {
59+
it(`Should ${shouldLog ? '' : 'not'} call \`console.${channel}\` if \`.${channel}\` is called`, () => {
60+
client[channel](message);
61+
expect(
62+
spies[`${channel}Spy`] && spies[`${channel}Spy`].called,
63+
`Expected ${channel} to ${shouldLog ? '' : 'not'} log`
64+
).to.be[shouldLog ? 'true' : 'false'];
65+
});
66+
}
67+
68+
describe('Class instance methods', () => {
69+
/**
70+
* Allow all logs to be exported for this block of tests
71+
*/
72+
before(() => {
73+
setLogLevel(LogLevel.DEBUG);
74+
});
75+
testLog(message, 'debug', true);
76+
testLog(message, 'log', true);
77+
testLog(message, 'info', true);
78+
testLog(message, 'warn', true);
79+
testLog(message, 'error', true);
80+
});
81+
82+
describe('Defaults', () => {
83+
before(() => {
84+
setLogLevel(LogLevel.WARN);
85+
});
86+
testLog(message, 'debug', false);
87+
testLog(message, 'log', false);
88+
testLog(message, 'info', false);
89+
testLog(message, 'warn', true);
90+
testLog(message, 'error', true);
91+
});
92+
});

0 commit comments

Comments
 (0)