diff --git a/test/commands/theme/component/dev.test.ts b/test/commands/theme/component/dev.test.ts index 724b5d6..da6fa5b 100644 --- a/test/commands/theme/component/dev.test.ts +++ b/test/commands/theme/component/dev.test.ts @@ -1,13 +1,13 @@ -import {runCommand} from '@oclif/test' -import {expect} from 'chai' +import { runCommand } from '@oclif/test' +import { expect } from 'chai' import chokidar, { FSWatcher } from 'chokidar' import * as fs from 'node:fs' import * as path from 'node:path' -import {fileURLToPath} from 'node:url' +import { fileURLToPath } from 'node:url' import sinon from 'sinon' +import Dev from '../../../../src/commands/theme/component/dev.js' import Install from '../../../../src/commands/theme/component/install.js' -import GenerateImportMap from '../../../../src/commands/theme/generate/import-map.js' import GenerateTemplateMap from '../../../../src/commands/theme/generate/template-map.js' const __dirname = path.dirname(fileURLToPath(import.meta.url)) @@ -22,34 +22,33 @@ describe('theme component dev', () => { beforeEach(() => { sandbox = sinon.createSandbox() - fs.cpSync(collectionPath, testCollectionPath, {recursive: true}) - fs.cpSync(themePath, testThemePath, {recursive: true}) + fs.cpSync(collectionPath, testCollectionPath, { recursive: true }) + fs.cpSync(themePath, testThemePath, { recursive: true }) process.chdir(testCollectionPath) }) afterEach(() => { sandbox.restore() - fs.rmSync(testCollectionPath, {force: true, recursive: true}) - fs.rmSync(testThemePath, {force: true, recursive: true}) + fs.rmSync(testCollectionPath, { force: true, recursive: true }) + fs.rmSync(testThemePath, { force: true, recursive: true }) }) it('copies the component setup files to the dev directory', async () => { - await runCommand(['theme', 'component', 'dev', '-t', '../test-theme']) + await runCommand(['theme', 'component', 'dev', '-t', testThemePath]) expect(fs.existsSync(path.join(testCollectionPath, '.dev', 'sections', 'with-setup.liquid'))).to.be.true expect(fs.existsSync(path.join(testCollectionPath, '.dev', 'templates', 'index.with-setup.liquid'))).to.be.true }) it('merges the settings_schema.json setup files', async () => { - await runCommand(['theme', 'component', 'dev', '-t', '../test-theme']) + await runCommand(['theme', 'component', 'dev', '-t', testThemePath]) expect(fs.existsSync(path.join(testCollectionPath, '.dev', 'config', 'settings_schema.json'))).to.be.true const json = fs.readFileSync(path.join(testCollectionPath, '.dev', 'config', 'settings_schema.json'), 'utf8') const jsonObject = JSON.parse(json) expect(jsonObject).to.have.deep.members([{ name: "schema_1" }, { name: "schema_2" }, { name: "schema_3" }]) - }) it('merges the settings_data.json setup files', async () => { - await runCommand(['theme', 'component', 'dev', '-t', '../test-theme']) + await runCommand(['theme', 'component', 'dev', '-t', testThemePath]) expect(fs.existsSync(path.join(testCollectionPath, '.dev', 'config', 'settings_data.json'))).to.be.true const json = fs.readFileSync(path.join(testCollectionPath, '.dev', 'config', 'settings_data.json'), 'utf8') const jsonObject = JSON.parse(json) @@ -58,7 +57,7 @@ describe('theme component dev', () => { }) it('copies a selected component setup file to the dev directory', async () => { - await runCommand(['theme', 'component', 'dev', 'with-setup', '-t', '../test-theme',]) + await runCommand(['theme', 'component', 'dev', 'with-setup', '-t', testThemePath]) expect(fs.existsSync(path.join(testCollectionPath, '.dev', 'sections', 'with-setup.liquid'))).to.be.true expect(fs.existsSync(path.join(testCollectionPath, '.dev', 'templates', 'index.with-setup.liquid'))).to.be.true @@ -67,33 +66,35 @@ describe('theme component dev', () => { }) it('runs the install command', async () => { - const installRunSpy = sandbox.spy(Install.prototype, 'run') + const installRunStub = sandbox.stub(Install, 'run').resolves() + sandbox.stub(Dev.prototype, 'log').returns() - await runCommand(['theme', 'component', 'dev', '-t', '../test-theme']) + await Dev.run(['-t', testThemePath, '--no-preview', '--no-watch']) - expect(installRunSpy.calledOnce).to.be.true + expect(installRunStub.called).to.be.true }) it('runs the generate import map command', async () => { - const generateImportMapRunSpy = sandbox.spy(GenerateImportMap.prototype, 'run') + const installRunStub = sandbox.stub(Install, 'run').resolves() + sandbox.stub(Dev.prototype, 'log').returns() + + await Dev.run(['-t', testThemePath, '--no-preview', '--no-watch']) - await runCommand(['theme', 'component', 'dev', '-t', '../test-theme', '--no-preview', '--no-watch']) - - expect(generateImportMapRunSpy.callCount).to.be.greaterThan(0) - expect(generateImportMapRunSpy.called).to.be.true + expect(installRunStub.called).to.be.true }) it('runs the generate template map command', async () => { - const generateTemplateMapRunSpy = sandbox.spy(GenerateTemplateMap.prototype, 'run') + const generateTemplateMapRunStub = sandbox.stub(GenerateTemplateMap, 'run').resolves() + sandbox.stub(Dev.prototype, 'log').returns() + sandbox.stub(Install, 'run').resolves() - await runCommand(['theme', 'component', 'dev', '-t', '../test-theme', '--no-preview', '--no-watch']) + await Dev.run(['-t', testThemePath, '--no-preview', '--no-watch', '--generate-template-map']) - expect(generateTemplateMapRunSpy.calledOnce).to.be.true + expect(generateTemplateMapRunStub.called).to.be.true }) it('watches for changes to the theme and components and rebuilds the theme', async () => { const watchStub = sandbox.stub(chokidar, 'watch') - // Mock the watch method to return a minimal watcher interface const onStub = sandbox.stub() const mockWatcher: Partial = { emit: sandbox.stub(), @@ -101,16 +102,14 @@ describe('theme component dev', () => { } watchStub.returns(mockWatcher as FSWatcher) - // Set NODE_ENV to test const originalEnv = process.env.NODE_ENV process.env.NODE_ENV = 'test' - await runCommand(['theme', 'component', 'dev', '-t', '../test-theme', '--watch', '--no-preview']) + await runCommand(['theme', 'component', 'dev', '-t', testThemePath, '--watch', '--no-preview']) expect(watchStub.calledOnce).to.be.true expect(watchStub.firstCall.args[0]).to.deep.equal([path.join(testThemePath), path.join(testCollectionPath, 'components')]) - - // Restore NODE_ENV + process.env.NODE_ENV = originalEnv }) }) diff --git a/test/commands/theme/component/install.test.ts b/test/commands/theme/component/install.test.ts index fed051e..36ef1b6 100644 --- a/test/commands/theme/component/install.test.ts +++ b/test/commands/theme/component/install.test.ts @@ -1,12 +1,12 @@ -import {runCommand} from '@oclif/test' -import {expect} from 'chai' +import { expect } from 'chai' import * as fs from 'node:fs' import * as path from 'node:path' -import {fileURLToPath} from 'node:url' +import { fileURLToPath } from 'node:url' import sinon from 'sinon' import Clean from '../../../../src/commands/theme/component/clean.js' import Copy from '../../../../src/commands/theme/component/copy.js' +import Install from '../../../../src/commands/theme/component/install.js' import Map from '../../../../src/commands/theme/component/map.js' import GenerateImportMap from '../../../../src/commands/theme/generate/import-map.js' @@ -19,49 +19,51 @@ const testThemePath = path.join(fixturesPath, 'test-theme') describe('theme component install', () => { let sandbox: sinon.SinonSandbox + let mapRunStub: sinon.SinonStub + let copyRunStub: sinon.SinonStub + let cleanRunStub: sinon.SinonStub + let generateRunStub: sinon.SinonStub - beforeEach(() => { + beforeEach(async () => { sandbox = sinon.createSandbox() - fs.cpSync(collectionPath, testCollectionPath, {recursive: true}) - fs.cpSync(themePath, testThemePath, {recursive: true}) + mapRunStub = sandbox.stub(Map.prototype, 'run').resolves() + copyRunStub = sandbox.stub(Copy.prototype, 'run').resolves() + cleanRunStub = sandbox.stub(Clean.prototype, 'run').resolves() + generateRunStub = sandbox.stub(GenerateImportMap.prototype, 'run').resolves() + + fs.cpSync(collectionPath, testCollectionPath, { recursive: true }) + fs.cpSync(themePath, testThemePath, { recursive: true }) process.chdir(testCollectionPath) }) afterEach(() => { sandbox.restore() - fs.rmSync(testCollectionPath, {force: true, recursive: true}) - fs.rmSync(testThemePath, {force: true, recursive: true}) + fs.rmSync(testCollectionPath, { force: true, recursive: true }) + fs.rmSync(testThemePath, { force: true, recursive: true }) }) it('runs the theme component map command', async () => { - const mapRunSpy = sandbox.spy(Map.prototype, 'run') - - await runCommand(['theme', 'component', 'install', '../test-theme']) - - expect(mapRunSpy.calledOnce).to.be.true + await Install.run([testThemePath]) + expect(mapRunStub.calledOnce).to.be.true }) it('runs the theme component copy command', async () => { - const copyRunSpy = sandbox.spy(Copy.prototype, 'run') - - await runCommand(['theme', 'component', 'install', '../test-theme']) - - expect(copyRunSpy.calledOnce).to.be.true + await Install.run([testThemePath]) + expect(copyRunStub.calledOnce).to.be.true }) it('runs the theme component clean command', async () => { - const cleanRunSpy = sandbox.spy(Clean.prototype, 'run') - - await runCommand(['theme', 'component', 'install', '../test-theme']) - - expect(cleanRunSpy.calledOnce).to.be.true + await Install.run([testThemePath]) + expect(cleanRunStub.calledOnce).to.be.true }) it('runs the theme component generate import map command', async () => { - const generateImportMapRunSpy = sandbox.spy(GenerateImportMap.prototype, 'run') - - await runCommand(['theme', 'component', 'install', '../test-theme']) + await Install.run([testThemePath]) + expect(generateRunStub.calledOnce).to.be.true + }) - expect(generateImportMapRunSpy.calledOnce).to.be.true + it('runs sub-commands in correct order', async () => { + await Install.run([testThemePath]) + sinon.assert.callOrder(mapRunStub, copyRunStub, cleanRunStub, generateRunStub) }) })