File size: 2,975 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, afterEach } from 'vitest';
import { debugLogger } from './debugLogger.js';

describe('DebugLogger', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('should call console.log with the correct arguments', () => {
    const spy = vi.spyOn(console, 'log').mockImplementation(() => {});
    const message = 'This is a log message';
    const data = { key: 'value' };
    debugLogger.log(message, data);
    expect(spy).toHaveBeenCalledWith(message, data);
    expect(spy).toHaveBeenCalledTimes(1);
  });

  it('should call console.warn with the correct arguments', () => {
    const spy = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const message = 'This is a warning message';
    const data = [1, 2, 3];
    debugLogger.warn(message, data);
    expect(spy).toHaveBeenCalledWith(message, data);
    expect(spy).toHaveBeenCalledTimes(1);
  });

  it('should call console.error with the correct arguments', () => {
    const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const message = 'This is an error message';
    const error = new Error('Something went wrong');
    debugLogger.error(message, error);
    expect(spy).toHaveBeenCalledWith(message, error);
    expect(spy).toHaveBeenCalledTimes(1);
  });

  it('should call console.debug with the correct arguments', () => {
    const spy = vi.spyOn(console, 'debug').mockImplementation(() => {});
    const message = 'This is a debug message';
    const obj = { a: { b: 'c' } };
    debugLogger.debug(message, obj);
    expect(spy).toHaveBeenCalledWith(message, obj);
    expect(spy).toHaveBeenCalledTimes(1);
  });

  it('should handle multiple arguments correctly for all methods', () => {
    const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
    const debugSpy = vi.spyOn(console, 'debug').mockImplementation(() => {});

    debugLogger.log('one', 2, true);
    expect(logSpy).toHaveBeenCalledWith('one', 2, true);

    debugLogger.warn('one', 2, false);
    expect(warnSpy).toHaveBeenCalledWith('one', 2, false);

    debugLogger.error('one', 2, null);
    expect(errorSpy).toHaveBeenCalledWith('one', 2, null);

    debugLogger.debug('one', 2, undefined);
    expect(debugSpy).toHaveBeenCalledWith('one', 2, undefined);
  });

  it('should handle calls with no arguments', () => {
    const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

    debugLogger.log();
    expect(logSpy).toHaveBeenCalledWith();
    expect(logSpy).toHaveBeenCalledTimes(1);

    debugLogger.warn();
    expect(warnSpy).toHaveBeenCalledWith();
    expect(warnSpy).toHaveBeenCalledTimes(1);
  });
});