<commit_before>import DeviceStateTracker from '../src/lib/DeviceStateTracker';
import DeviceStateEvent from '../src/lib/DeviceStateEvent';
import { mediaQueries, DeviceState, WrongDeviceState } from './configMock';
import { expect } from 'chai';
import {} from 'mocha';
const matchMediaMock = require('match-media-mock').create();


window.matchMedia = <any> matchMediaMock;

describe('DeviceStateTracker', () => {
	describe('#DeviceStateTracker should return the correct state for different Device States', () => {
		const deviceStateTracker:DeviceStateTracker = new DeviceStateTracker(mediaQueries, DeviceState);
		// https://github.com/azazdeaz/match-media-mock/issues/2
		(<any> deviceStateTracker)._queryLists.forEach((mq, index) => mq.media = mediaQueries[DeviceState[index]]);

		it('should match X_SMALL', () => {
			matchMediaMock.setConfig({ type: 'screen', width: 479 });

			const eventHandler = () => {
				expect(deviceStateTracker.currentState).to.equal(DeviceState.X_SMALL);
				deviceStateTracker.removeEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);
			};

			deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);

			(<any> deviceStateTracker)._queryLists.forEach(mq => mq.callListeners());
		});

		it('should match SMALL', () => {
			matchMediaMock.setConfig({ type: 'screen', width: 480 });

			const eventHandler = () => {
				expect(deviceStateTracker.currentState).to.equal(DeviceState.SMALL);
				deviceStateTracker.removeEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);
			};

			deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);

			(<any> deviceStateTracker)._queryLists.forEach(mq => mq.callListeners());
		});

		it('should match MEDIUM', () => {
			matchMediaMock.setConfig({ type: 'screen', width: 768 });

			const eventHandler = () => {
				expect(deviceStateTracker.currentState).to.equal(DeviceState.MEDIUM);
				deviceStateTracker.removeEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);
			};

			deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);

			(<any> deviceStateTracker)._queryLists.forEach(mq => mq.callListeners());
		});

		it('should match LARGE', () => {
			matchMediaMock.setConfig({ type: 'screen', width: 1280 });

			const eventHandler = () => {
				expect(deviceStateTracker.currentState).to.equal(DeviceState.LARGE);
				deviceStateTracker.removeEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);
			};

			deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);

			(<any> deviceStateTracker)._queryLists.forEach(mq => mq.callListeners());
		});
	});

	it('should return the state-indicator div', () => {
		const deviceStateTracker:DeviceStateTracker = new DeviceStateTracker(mediaQueries, DeviceState, false, true);
		// Lint will throw an error if not defined (workaround)
		if (deviceStateTracker) {
			const stateIndicator = document.querySelector('.seng-state-indicator').tagName;
			expect(stateIndicator).to.equal('DIV');
		}
	});

	it('should throw an error while calling construct with wrong enum', () => {
		expect(() => {
			new DeviceStateTracker(mediaQueries, WrongDeviceState);
		}).to.throw(
			Error,
		);
	});

	it('should throw an error when device state doesn\'t have any keys', () => {
		expect(() => {
			new DeviceStateTracker(mediaQueries, {});
		}).to.throw(
			Error,
		);
	});

	it('should throw an error when device state isn\'t found in mediaQueries', () => {
		const mediaQueriesClone = JSON.parse(JSON.stringify(mediaQueries));
		delete mediaQueriesClone['MEDIUM'];<commit_msg>Fix TS warning, object possible null<commit_after>import DeviceStateTracker from '../src/lib/DeviceStateTracker';
import DeviceStateEvent from '../src/lib/DeviceStateEvent';
import { mediaQueries, DeviceState, WrongDeviceState } from './configMock';
import { expect } from 'chai';
import {} from 'mocha';
const matchMediaMock = require('match-media-mock').create();


window.matchMedia = <any> matchMediaMock;

describe('DeviceStateTracker', () => {
	describe('#DeviceStateTracker should return the correct state for different Device States', () => {
		const deviceStateTracker:DeviceStateTracker = new DeviceStateTracker(mediaQueries, DeviceState);
		// https://github.com/azazdeaz/match-media-mock/issues/2
		(<any> deviceStateTracker)._queryLists.forEach((mq, index) => mq.media = mediaQueries[DeviceState[index]]);

		it('should match X_SMALL', () => {
			matchMediaMock.setConfig({ type: 'screen', width: 479 });

			const eventHandler = () => {
				expect(deviceStateTracker.currentState).to.equal(DeviceState.X_SMALL);
				deviceStateTracker.removeEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);
			};

			deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);

			(<any> deviceStateTracker)._queryLists.forEach(mq => mq.callListeners());
		});

		it('should match SMALL', () => {
			matchMediaMock.setConfig({ type: 'screen', width: 480 });

			const eventHandler = () => {
				expect(deviceStateTracker.currentState).to.equal(DeviceState.SMALL);
				deviceStateTracker.removeEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);
			};

			deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);

			(<any> deviceStateTracker)._queryLists.forEach(mq => mq.callListeners());
		});

		it('should match MEDIUM', () => {
			matchMediaMock.setConfig({ type: 'screen', width: 768 });

			const eventHandler = () => {
				expect(deviceStateTracker.currentState).to.equal(DeviceState.MEDIUM);
				deviceStateTracker.removeEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);
			};

			deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);

			(<any> deviceStateTracker)._queryLists.forEach(mq => mq.callListeners());
		});

		it('should match LARGE', () => {
			matchMediaMock.setConfig({ type: 'screen', width: 1280 });

			const eventHandler = () => {
				expect(deviceStateTracker.currentState).to.equal(DeviceState.LARGE);
				deviceStateTracker.removeEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);
			};

			deviceStateTracker.addEventListener(DeviceStateEvent.STATE_UPDATE, eventHandler);

			(<any> deviceStateTracker)._queryLists.forEach(mq => mq.callListeners());
		});
	});

	it('should return the state-indicator div', () => {
		const deviceStateTracker:DeviceStateTracker = new DeviceStateTracker(mediaQueries, DeviceState, false, true);
		// Lint will throw an error if not defined (workaround)
		if (deviceStateTracker) {
			expect(document.querySelector('.seng-state-indicator')!.tagName).to.equal('DIV');
		}
	});

	it('should throw an error while calling construct with wrong enum', () => {
		expect(() => {
			new DeviceStateTracker(mediaQueries, WrongDeviceState);
		}).to.throw(
			Error,
		);
	});

	it('should throw an error when device state doesn\'t have any keys', () => {
		expect(() => {
			new DeviceStateTracker(mediaQueries, {});
		}).to.throw(
			Error,
		);
	});

	it('should throw an error when device state isn\'t found in mediaQueries', () => {
		const mediaQueriesClone = JSON.parse(JSON.stringify(mediaQueries));
		delete mediaQueriesClone['MEDIUM'];
{'fraction_non_alphanumeric': 0.14246196403872752, 'fraction_numerical': 0.003872752420470263, 'mean_word_length': 5.3597185576077395, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 57, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4867976', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2044, 'n_words': 393}