json Chai e Sinon Para checkar console.log

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json Chai e Sinon Para checkar console.log相关的知识,希望对你有一定的参考价值。

import { expect, assert } from 'chai';
import sinon from 'sinon';
import { createDefaultDOM } from '../js-dom/components';
import PsChatClient from '../../src/PsChatClient';

describe('PsChatClient', () => {
	let appId = 'avi';
	let psChatClient;
	global.document = createDefaultDOM(appId);

	beforeEach(() => {
		psChatClient = new PsChatClient();
	});

	describe('_getAppId()', () => {
		it('#Deve retornar o id da aplicação inserida no atributo "data-chat-app" do elemento script', () => {
			expect(psChatClient._getAppId()).to.equal(appId);
		});
	});

	describe('_insertChatScript(type, src)', () => {
		it('#Deve inserir um script no DOM com um id e um src passados por parâmetro', () => {
			const chatScriptId = 'blip';

			psChatClient._insertChatScript(chatScriptId, `${chatScriptId}.js`);
			const chatScript = global.document.querySelector(`#${chatScriptId}`);
			expect(chatScript).to.exist;
		});
	});

	describe('_initializeChat(type, chatData)', () => {
		let chatData = {};

		beforeEach(() => {
			sinon.spy(console, 'log');
		});
		afterEach(() => {
			console.log.restore();
		});

		it('#Deve executar o inicializador com o tipo de chat GV', function() {
			assert(!console.log.called);
			psChatClient._initializeChat('gv', chatData);
			assert(console.log.called);
			assert(console.log.calledWith('#Deve executar o inicializador com o tipo de chat GV'));
		});

		it('#Deve executar o inicializador com o tipo de chat AVI', function() {
			assert(!console.log.called);
			psChatClient._initializeChat('avi', chatData);
			assert(console.log.called);
			assert(console.log.calledWith('#Deve executar o inicializador com o tipo de chat AVI'));
		});

		it('#Deve executar o inicializador com o tipo de chat BLIP', function() {
			assert(!console.log.called);
			psChatClient._initializeChat('blip', chatData);
			assert(console.log.called);
			assert(console.log.calledWith('#Deve executar o inicializador com o tipo de chat BLIP'));
		});
	});
});
import { JSDOM } from 'jsdom';

export const createDefaultDOM = appId => new JSDOM(`
    <!DOCTYPE html>
    <html>
        <head></head>
        <body>
            <script data-chat-app="${appId}" src="test.js"></script>
        </body>
    </html>
`).window.document;
import PsChatAPIService from './services/PsChatAPIService';
import blipInitializer from './chat-initializers/blip-initializer';
import aviInitializer from './chat-initializers/avi-initializer';
import gvInitializer from './chat-initializers/gv-initializer';

export default class PsChatClient {
	constructor() {
		this._appId = this._getAppId();
		this._psChatAPIService = new PsChatAPIService('http://localhost:3000/blip/1');
		this._chatInfo = {};
	}

	async init() {
		try {
			this._chatInfo = await this._psChatAPIService.getChatInfo(this._appId);
		} catch (err) {
			throw err;
		}

		const { type, src, data } = this._chatInfo;

		if (type !== 'avi') {
			this._insertChatScript(type, src);
		}

		this._initializeChat(type, data);
	}

	_getAppId() {
		const chatScriptElem = document.currentScript
			? document.currentScript
			: document.querySelector('script[data-chat-app]');
		return chatScriptElem.dataset.chatApp;
	}

	_insertChatScript(type, src) {
		const chatScript = document.createElement('script');
		chatScript.id = type;
		chatScript.src = src;
		document.querySelector('body').appendChild(chatScript);
	}

	_initializeChat(type, chatData) {
		switch (type) {
			case 'gv':
				gvInitializer('#gv', chatData);
				break;
			case 'avi':
				aviInitializer(chatData);
				break;
			case 'blip':
			default:
				blipInitializer('#blip', chatData);
		}
	}
}
export default function(selector, chatData) {
	console.log('#Deve executar o inicializador com o tipo de chat GV');
}

"scripts": {
    "test": "mocha --require babel-register test/**/*-spec.js -r jsdom-global/register --recursive --watch",
}

以上是关于json Chai e Sinon Para checkar console.log的主要内容,如果未能解决你的问题,请参考以下文章

用 sinon 存根,用 chai 测试

Sinon chai 否定断言不适用于期望

markdown 节点单元测试备忘单:Mocha,Chai和Sinon

markdown Mocha,Chai和Sinon的终极单元测试作弊表

markdown Mocha,Chai和Sinon的终极单元测试作弊表

如何正确指定使用 ts-sinon 的模拟的两种类型?