Skip to content

Commit b1a1beb

Browse files
committed
chore: added test for viewmodel generator
1 parent aeb89ae commit b1a1beb

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { ViewModelWizardData } from 'wizard/ViewModelWizard';
2+
import * as assert from 'assert';
3+
import { Uri } from 'vscode';
4+
import ViewModelClassGenerator from 'generator/viewModel/ViewModelClassGenerator';
5+
import { describe, it, before, afterEach } from 'mocha';
6+
import { setup } from 'test/setup';
7+
import { getReferenceFile, getTestWorkspaceUri } from 'test/util';
8+
import FileHeader from 'common/php/FileHeader';
9+
import sinon from 'sinon';
10+
11+
describe('ViewModelClassGenerator Tests', () => {
12+
const viewModelWizardData: ViewModelWizardData = {
13+
module: 'Foo_Bar',
14+
className: 'TestViewModel',
15+
directory: 'ViewModel',
16+
};
17+
18+
before(async () => {
19+
await setup();
20+
});
21+
22+
afterEach(() => {
23+
sinon.restore();
24+
});
25+
26+
it('should generate viewModel class file', async () => {
27+
// Mock the FileHeader.getHeader method to return a consistent header
28+
sinon.stub(FileHeader, 'getHeader').returns('Foo_Bar');
29+
30+
// Create the generator with test data
31+
const generator = new ViewModelClassGenerator(viewModelWizardData);
32+
33+
// Use a test workspace URI
34+
const workspaceUri = getTestWorkspaceUri();
35+
36+
// Generate the file
37+
const generatedFile = await generator.generate(workspaceUri);
38+
39+
// Get the reference file content
40+
const referenceContent = getReferenceFile('generator/viewModel/TestViewModel.php');
41+
42+
// Compare the generated content with reference
43+
assert.strictEqual(generatedFile.content, referenceContent);
44+
});
45+
46+
it('should generate file in correct location', async () => {
47+
// Create the generator with test data
48+
const generator = new ViewModelClassGenerator(viewModelWizardData);
49+
50+
// Use a test workspace URI
51+
const workspaceUri = getTestWorkspaceUri();
52+
53+
// Generate the file
54+
const generatedFile = await generator.generate(workspaceUri);
55+
56+
// Expected path
57+
const expectedPath = Uri.joinPath(
58+
workspaceUri,
59+
'app/code/Foo/Bar/ViewModel/TestViewModel.php'
60+
).fsPath;
61+
62+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
63+
});
64+
65+
it('should generate viewModel class in custom directory', async () => {
66+
// Create test data with custom directory
67+
const customDirectoryData: ViewModelWizardData = {
68+
...viewModelWizardData,
69+
directory: 'ViewModel/Custom/Path',
70+
};
71+
72+
// Mock the FileHeader.getHeader method to return a consistent header
73+
sinon.stub(FileHeader, 'getHeader').returns('Foo_Bar');
74+
75+
// Create the generator with custom directory data
76+
const generator = new ViewModelClassGenerator(customDirectoryData);
77+
78+
// Use a test workspace URI
79+
const workspaceUri = getTestWorkspaceUri();
80+
81+
// Generate the file
82+
const generatedFile = await generator.generate(workspaceUri);
83+
84+
// Get the reference file content for custom directory
85+
const referenceContent = getReferenceFile('generator/viewModel/TestViewModelCustomPath.php');
86+
87+
// Compare the generated content with reference
88+
assert.strictEqual(generatedFile.content, referenceContent);
89+
90+
// Verify file location
91+
const expectedPath = Uri.joinPath(
92+
workspaceUri,
93+
'app/code/Foo/Bar/ViewModel/Custom/Path/TestViewModel.php'
94+
).fsPath;
95+
96+
assert.strictEqual(generatedFile.uri.fsPath, expectedPath);
97+
});
98+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Foo_Bar
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Foo\Bar\ViewModel;
10+
11+
use Magento\Framework\View\Element\Block\ArgumentInterface;
12+
13+
class TestViewModel implements ArgumentInterface
14+
{
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Foo_Bar
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Foo\Bar\ViewModel\Custom\Path;
10+
11+
use Magento\Framework\View\Element\Block\ArgumentInterface;
12+
13+
class TestViewModel implements ArgumentInterface
14+
{
15+
}

0 commit comments

Comments
 (0)