Skip to content

Commit f44af55

Browse files
committed
AC-1156: Move custom eslint tests to magento-coding-standard repo
1 parent ec62c5c commit f44af55

33 files changed

+3118
-0
lines changed

.github/workflows/php.yml

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
name: Tests with PHP ${{ matrix.php-version }} and ${{ matrix.dependencies }} dependencies
2626

2727
steps:
28+
- name: Setup node
29+
uses: actions/setup-node@v2
30+
2831
- uses: actions/checkout@v2
2932

3033
- name: Setup PHP
@@ -34,6 +37,11 @@ jobs:
3437
env:
3538
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3639

40+
- name: Install dependencies
41+
run: npm install
42+
- name: Run ESLint
43+
run: npm run eslint -- eslint/rules
44+
3745
- name: Validate composer
3846
run: composer validate
3947

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/vendor/*
22
/bin/*
3+
/node_modules
34

45
# IDE files
56
.idea/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
use PHPUnit\Framework\TestCase;
11+
12+
/**
13+
* Abstract class AbstractEslintTestCase
14+
*
15+
* Test Eslint Rules (magento-coding-standard/eslint/rules)
16+
*/
17+
abstract class AbstractEslintTestCase extends TestCase
18+
{
19+
/**
20+
* @param string $testFile
21+
* @param array $expectedMessages
22+
*/
23+
protected function assertFileContainsError(string $testFile, array $expectedMessages)
24+
{
25+
exec(
26+
'npm run eslint -- Magento2/Tests/Eslint/' . $testFile,
27+
$output
28+
);
29+
30+
foreach ($expectedMessages as $message) {
31+
$this->assertStringContainsString($message, implode(' ',$output));
32+
}
33+
}
34+
}

Magento2/Tests/Eslint/AndSelfTest.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$(document).ready(function() {
2+
'use strict';
3+
$("div").find("p").andSelf().addClass("border");
4+
});

Magento2/Tests/Eslint/AndSelfTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class AndSelfTest
12+
*
13+
* Test Eslint Rule: jquery-no-andSelf.js
14+
*/
15+
class AndSelfTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute()
18+
{
19+
$this->assertFileContainsError(
20+
'AndSelfTest.js',
21+
['jQuery.andSelf() removed, use jQuery.addBack()']
22+
);
23+
}
24+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$(document).ready(function(){
2+
'use strict';
3+
$(".btn1").bind("click");
4+
});
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class BindUnbindTest
12+
*
13+
* Test Eslint Rule: jquery-no-bind-unbind.js
14+
*/
15+
class BindUnbindTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute()
18+
{
19+
$this->assertFileContainsError(
20+
'BindUnbindTest.js',
21+
['jQuery $.bind and $.unbind are deprecated, use $.on and $.off instead']
22+
);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$(document).ready(function(){
2+
'use strict';
3+
$("input").blur();
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class ClickEventShorthandTest
12+
*
13+
* Test Eslint Rule: jquery-no-click-event-shorthand.js
14+
*/
15+
class ClickEventShorthandTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute()
18+
{
19+
$this->assertFileContainsError(
20+
'ClickEventShorthand.js',
21+
['Instead of .blur(fn) use .on("blur", fn). Instead of .blur() use .trigger("blur")']
22+
);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$(document).ready(function(){
2+
'use strict';
3+
$( "table" ).delegate( "td", "click", function() {
4+
$( this ).toggleClass( "chosen" );
5+
});
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class DelegateUndelegateTest
12+
*
13+
* Test Eslint Rule: jquery-no-delegate-undelegate.js
14+
*/
15+
class DelegateUndelegateTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute()
18+
{
19+
$this->assertFileContainsError(
20+
'DelegateUndelegateTest.js',
21+
['jQuery $.delegate and $.undelegate are deprecated, use $.on and $.off instead']
22+
);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$(document).ready(function(){
2+
'use strict';
3+
$( "#result" ).load( "ajax/test.html" );
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class EventShorthandTest
12+
*
13+
* Test Eslint Rule: jquery-no-event-shorthand.js
14+
*/
15+
class EventShorthandTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute()
18+
{
19+
$this->assertFileContainsError(
20+
'EventShorthandTest.js',
21+
['jQuery.load() was removed, use .on("load", fn) instead']
22+
);
23+
}
24+
}

Magento2/Tests/Eslint/SizeTest.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$(document).ready(function(){
2+
'use strict';
3+
$("div").size();
4+
});

Magento2/Tests/Eslint/SizeTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class SizeTest
12+
*
13+
* Test Eslint Rule: jquery-no-size.js
14+
*/
15+
class SizeTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute()
18+
{
19+
$this->assertFileContainsError(
20+
'SizeTest.js',
21+
['jQuery.size() removed, use jQuery.length']
22+
);
23+
}
24+
}

Magento2/Tests/Eslint/TrimTest.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$(document).ready(function(){
2+
'use strict';
3+
$.trim(" hello, how are you? ");
4+
});

Magento2/Tests/Eslint/TrimTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\Eslint;
9+
10+
/**
11+
* Class TrimTest
12+
*
13+
* Test Eslint Rule: jquery-no-trim.js
14+
*/
15+
class TrimTest extends AbstractEslintTestCase
16+
{
17+
public function testExecute()
18+
{
19+
$this->assertFileContainsError(
20+
'TrimTest.js',
21+
['jQuery.trim is deprecated; use String.prototype.trim']
22+
);
23+
}
24+
}

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ Also, verify that the sniffer code itself is written according to the Magento Co
9999
vendor/bin/phpcs --standard=Magento2 Magento2/ --extensions=php
100100
```
101101

102+
### ESLint testing
103+
Prerequisites: [Node.js](https://nodejs.org/) (`^12.22.0`, `^14.17.0`, or `>=16.0.0`).
104+
105+
You need to run the following command to install all the necessary packages described in the `package.json` file:
106+
```bash
107+
npm install
108+
```
109+
110+
After that, you can just execute the ESLint tests simply running:
111+
```bash
112+
npm run eslint -- eslint/rules
113+
```
102114
## License
103115

104116
Each Magento source file included in this distribution is licensed under the OSL-3.0 license.

eslint/.eslintrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": [
3+
"./.eslintrc-reset",
4+
"./.eslintrc-magento",
5+
"./.eslintrc-jquery",
6+
"./.eslintrc-misc"
7+
]
8+
}

eslint/.eslintrc-jquery

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"rules": {
3+
"jquery-no-andSelf": 2,
4+
"jquery-no-bind-unbind": 2,
5+
"jquery-no-click-event-shorthand": 2,
6+
"jquery-no-delegate-undelegate": 2,
7+
"jquery-no-event-shorthand": 2,
8+
"jquery-no-size": 2,
9+
"jquery-no-trim": 2
10+
}
11+
}

0 commit comments

Comments
 (0)