frontispiece / packages / ink-processor / src / data / story-section.spec.ts
story-section.spec.ts
Raw
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import { describe, it, expect } from "vitest"
import { advanceSectionLine, getVisibleLinesInSection } from "./story-section"
import { VisualInkSection } from "../types"
import { createTestSection } from "../test/test-data"

describe("advanceSectionLine", () => {
	it("should produce a new object", () => {
		const testSection: VisualInkSection = createTestSection()
		const newSection = advanceSectionLine(testSection)

		expect(newSection === testSection).toBe(false)
		expect(newSection.activeLine).toEqual(testSection.activeLine! + 1)
	})

	it("should should keep the actual internal objects the same", () => {
		const testSection: VisualInkSection = createTestSection()
		const newSection = advanceSectionLine(testSection)

		expect(newSection.choices).toStrictEqual(testSection.choices)
		expect(newSection.lines).toStrictEqual(testSection.lines)
	})

	it("should do nothing if there is no activeLine", () => {
		const testSection: VisualInkSection = createTestSection()
		testSection.activeLine = undefined
		const newSection = advanceSectionLine(testSection)

		expect(newSection === testSection).toBe(true)
		expect(newSection.activeLine).toEqual(testSection.activeLine)
	})
})

describe("getVisibleLinesInSection", () => {
	it("should retrieve only visible lines", () => {
		const testSection: VisualInkSection = createTestSection()
		testSection.activeLine = 0
		const visibleLines1 = getVisibleLinesInSection(testSection, {})
		expect(visibleLines1.length).toEqual(1)
		testSection.activeLine = 1
		const visibleLines2 = getVisibleLinesInSection(testSection, {})
		expect(visibleLines2.length).toEqual(2)

		// check to make sure that the lines are the same if they haven't changed
		expect(visibleLines1[0]).toBe(visibleLines2[0])

		testSection.lines[0].isHidden = true
		const visibleLines3 = getVisibleLinesInSection(testSection, {})
		expect(visibleLines3.length).toEqual(1)
		expect(visibleLines3[0]).toBe(visibleLines2[1])
	})

	it("should retrieve hidden lines if the option is set", () => {
		const testSection: VisualInkSection = createTestSection()
		testSection.activeLine = 3
		testSection.lines[0].isHidden = true
		const visibleLines = getVisibleLinesInSection(testSection, {showHidden: true})
		expect(visibleLines.length).toEqual(4)
	})

	it("should retrieve choices if options allow and section is complete", () => {
		const testSection: VisualInkSection = createTestSection()
		testSection.activeLine = 0
		const visibleLines1 = getVisibleLinesInSection(testSection, {
			showChosenChoice: true
		})
		expect(visibleLines1.length).toEqual(1)
		testSection.activeLine = testSection.lines.length - 1;
		const visibleLines2 = getVisibleLinesInSection(testSection, {
			showChosenChoice: true
		})
		expect(visibleLines2.length).toEqual(testSection.lines.length)

		testSection.activeChoiceIndex = 1
		const visibleLines3 = getVisibleLinesInSection(testSection, {
			showChosenChoice: true
		})
		expect(visibleLines3.length).toEqual(testSection.lines.length + 1)
		expect(visibleLines3[visibleLines3.length - 1].type).toEqual('choice')
	})
})