"use strict";

const {
	addLocalModule,
	getLocalModule
} = require("../lib/dependencies/LocalModulesHelpers");

describe("LocalModulesHelpers", () => {
	describe("addLocalModule", () => {
		it("returns a module var without special characters", () => {
			const state =
				/** @type {import("../lib/javascript/JavascriptParser").JavascriptParserState} */ (
					/** @type {unknown} */ ({
						localModules: ["first", "second"]
					})
				);
			const localModule = addLocalModule(state, "local_module_sample");
			expect(localModule).toBeInstanceOf(Object);
			expect(localModule).toMatchObject({
				name: "local_module_sample",
				idx: 2,
				used: false
			});
			expect(state.localModules).toHaveLength(3);
		});
	});

	describe("getLocalModule", () => {
		it("returns `null` if names information doesn't match", () => {
			const state =
				/** @type {import("../lib/javascript/JavascriptParser").JavascriptParserState} */ (
					/** @type {unknown} */ ({
						module: "module_sample",
						localModules: [
							{
								name: "first"
							},
							{
								name: "second"
							}
						]
					})
				);
			expect(getLocalModule(state, "local_module_sample")).toBeNull();
		});

		it("returns local module information", () => {
			const state =
				/** @type {import("../lib/javascript/JavascriptParser").JavascriptParserState} */ (
					/** @type {unknown} */ ({
						module: "module_sample",
						localModules: [
							{
								name: "first"
							},
							{
								name: "second"
							}
						]
					})
				);
			expect(getLocalModule(state, "first")).toEqual({
				name: "first"
			});
		});
	});
});
