Skip to content

Commit 940939c

Browse files
committed
Add additional tests for interaction of alphabetical sort and newline rules
1 parent fe1440d commit 940939c

File tree

1 file changed

+349
-0
lines changed

1 file changed

+349
-0
lines changed

tests/src/rules/order.js

Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,13 @@ ruleTester.run('order', rule, {
453453
`,
454454
options: [{ 'sort': 'ignore' }],
455455
}),
456+
// Ignore alphabetical order should be default
457+
test({
458+
code: `
459+
import path from 'path';
460+
import fs from 'fs';
461+
`,
462+
}),
456463
// Ignore alphabetical order across groups
457464
test({
458465
code: `
@@ -461,6 +468,146 @@ ruleTester.run('order', rule, {
461468
`,
462469
options: [{ 'sort': 'alphabetical' }],
463470
}),
471+
// Enforce alphabetical order with newlines-between: 'always'
472+
test({
473+
code: `
474+
var fs = require('fs');
475+
var path = require('path');
476+
477+
var async = require('async');
478+
var react = require('react');
479+
480+
var barParent = require('../bar');
481+
var fooParent = require('../foo');
482+
483+
var barSibling = require('./bar');
484+
var fooSibling = require('./foo');
485+
486+
var index = require('./');
487+
`,
488+
options: [
489+
{
490+
'newlines-between': 'always',
491+
'sort': 'alphabetical',
492+
},
493+
],
494+
}),
495+
// Ignore alphabetical order with newlines-between: 'always'
496+
test({
497+
code: `
498+
var path = require('path');
499+
var fs = require('fs');
500+
501+
var react = require('react');
502+
var async = require('async');
503+
504+
var fooParent = require('../foo');
505+
var barParent = require('../bar');
506+
507+
var fooSibling = require('./foo');
508+
var barSibling = require('./bar');
509+
510+
var index = require('./');
511+
`,
512+
options: [
513+
{
514+
'newlines-between': 'always',
515+
'sort': 'ignore',
516+
},
517+
],
518+
}),
519+
// Enforce alphabetical with newlines-between: 'never'
520+
test({
521+
code: `
522+
var fs = require('fs');
523+
var path = require('path');
524+
var async = require('async');
525+
var react = require('react');
526+
var barParent = require('../bar');
527+
var fooParent = require('../foo');
528+
var barSibling = require('./bar');
529+
var fooSibling = require('./foo');
530+
var index = require('./');
531+
`,
532+
options: [
533+
{
534+
'newlines-between': 'never',
535+
'sort': 'alphabetical',
536+
},
537+
],
538+
}),
539+
// Ignore alphabetical with newlines-between: 'never'
540+
test({
541+
code: `
542+
var path = require('path');
543+
var fs = require('fs');
544+
var react = require('react');
545+
var async = require('async');
546+
var fooParent = require('../foo');
547+
var barParent = require('../bar');
548+
var fooSibling = require('./foo');
549+
var barSibling = require('./bar');
550+
var index = require('./');
551+
`,
552+
options: [
553+
{
554+
'newlines-between': 'never',
555+
'sort': 'ignore',
556+
},
557+
],
558+
}),
559+
// Enforce alphabetical order with newlines-between: 'always-and-inside-groups'
560+
test({
561+
code: `
562+
var fs = require('fs');
563+
var path = require('path');
564+
565+
var util = require('util');
566+
567+
var async = require('async');
568+
var react = require('react');
569+
570+
var barParent = require('../bar');
571+
var fooParent = require('../foo');
572+
573+
var barSibling = require('./bar');
574+
var fooSibling = require('./foo');
575+
576+
var foobarSibling = require('./foobar');
577+
`,
578+
options: [
579+
{
580+
'newlines-between': 'always-and-inside-groups',
581+
'sort': 'alphabetical',
582+
},
583+
]
584+
}),
585+
// Ignore alphabetical order with newlines-between: 'always-and-inside-groups'
586+
test({
587+
code: `
588+
var path = require('path');
589+
var util = require('util');
590+
591+
var fs = require('fs');
592+
593+
var react = require('react');
594+
var async = require('async');
595+
596+
var fooParent = require('../foo');
597+
var barParent = require('../bar');
598+
599+
var foobarSibling = require('./foobar');
600+
601+
var fooSibling = require('./foo');
602+
var barSibling = require('./bar');
603+
`,
604+
options: [
605+
{
606+
'newlines-between': 'always-and-inside-groups',
607+
'sort': 'ignore',
608+
},
609+
]
610+
}),
464611
],
465612
invalid: [
466613
// builtin before external module (require)
@@ -991,5 +1138,207 @@ ruleTester.run('order', rule, {
9911138
},
9921139
],
9931140
}),
1141+
// Bad alphabetical order with newlines-between: 'always'
1142+
test({
1143+
code: `
1144+
var path = require('path');
1145+
var fs = require('fs');
1146+
1147+
var react = require('react');
1148+
var async = require('async');
1149+
1150+
var fooParent = require('../foo');
1151+
var barParent = require('../bar');
1152+
1153+
var fooSibling = require('./foo');
1154+
var barSibling = require('./bar');
1155+
1156+
var index = require('./');
1157+
`,
1158+
options: [
1159+
{
1160+
'newlines-between': 'always',
1161+
'sort': 'alphabetical',
1162+
},
1163+
],
1164+
errors: [
1165+
{
1166+
ruleId: 'order',
1167+
message: '`fs` import should occur before import of `path`',
1168+
},
1169+
{
1170+
ruleId: 'order',
1171+
message: '`async` import should occur before import of `react`',
1172+
},
1173+
{
1174+
ruleId: 'order',
1175+
message: '`../bar` import should occur before import of `../foo`',
1176+
},
1177+
{
1178+
ruleId: 'order',
1179+
message: '`./bar` import should occur before import of `./foo`',
1180+
},
1181+
],
1182+
}),
1183+
// Bad alphabetical order with newlines-between: 'always' and missing newline
1184+
test({
1185+
code: `
1186+
var fs = require('fs');
1187+
var react = require('react');
1188+
var async = require('async');
1189+
`,
1190+
options: [
1191+
{
1192+
'newlines-between': 'always',
1193+
'sort': 'alphabetical',
1194+
},
1195+
],
1196+
errors: [
1197+
{
1198+
ruleId: 'order',
1199+
message: 'There should be at least one empty line between import groups',
1200+
},
1201+
{
1202+
ruleId: 'order',
1203+
message: '`async` import should occur before import of `react`',
1204+
},
1205+
],
1206+
}),
1207+
// Enforce alphabetical order with newlines-between: 'never'
1208+
test({
1209+
code: `
1210+
var path = require('path');
1211+
var fs = require('fs');
1212+
var react = require('react');
1213+
var async = require('async');
1214+
var fooParent = require('../foo');
1215+
var barParent = require('../bar');
1216+
var fooSibling = require('./foo');
1217+
var barSibling = require('./bar');
1218+
var index = require('./');
1219+
`,
1220+
options: [
1221+
{
1222+
'newlines-between': 'never',
1223+
'sort': 'alphabetical',
1224+
},
1225+
],
1226+
errors: [
1227+
{
1228+
ruleId: 'order',
1229+
message: '`fs` import should occur before import of `path`',
1230+
},
1231+
{
1232+
ruleId: 'order',
1233+
message: '`async` import should occur before import of `react`',
1234+
},
1235+
{
1236+
ruleId: 'order',
1237+
message: '`../bar` import should occur before import of `../foo`',
1238+
},
1239+
{
1240+
ruleId: 'order',
1241+
message: '`./bar` import should occur before import of `./foo`',
1242+
},
1243+
],
1244+
}),
1245+
// Bad alphabetical order with newlines-between: 'never' and extra newline
1246+
test({
1247+
code: `
1248+
var fs = require('fs');
1249+
1250+
var react = require('react');
1251+
var async = require('async');
1252+
`,
1253+
options: [
1254+
{
1255+
'newlines-between': 'never',
1256+
'sort': 'alphabetical',
1257+
},
1258+
],
1259+
errors: [
1260+
{
1261+
ruleId: 'order',
1262+
message: 'There should be no empty line between import groups',
1263+
},
1264+
{
1265+
ruleId: 'order',
1266+
message: '`async` import should occur before import of `react`',
1267+
},
1268+
],
1269+
}),
1270+
// Enforce alphabetical order with newlines-between: 'always-and-inside-groups'
1271+
test({
1272+
code: `
1273+
var path = require('path');
1274+
var util = require('util');
1275+
1276+
var fs = require('fs');
1277+
1278+
var react = require('react');
1279+
var async = require('async');
1280+
1281+
var fooParent = require('../foo');
1282+
var barParent = require('../bar');
1283+
1284+
var foobarSibling = require('./foobar');
1285+
1286+
var fooSibling = require('./foo');
1287+
var barSibling = require('./bar');
1288+
`,
1289+
options: [
1290+
{
1291+
'newlines-between': 'always-and-inside-groups',
1292+
'sort': 'alphabetical',
1293+
},
1294+
],
1295+
errors: [
1296+
{
1297+
ruleId: 'order',
1298+
message: '`fs` import should occur before import of `path`',
1299+
},
1300+
{
1301+
ruleId: 'order',
1302+
message: '`async` import should occur before import of `react`',
1303+
},
1304+
{
1305+
ruleId: 'order',
1306+
message: '`../bar` import should occur before import of `../foo`',
1307+
},
1308+
{
1309+
ruleId: 'order',
1310+
message: '`./foo` import should occur before import of `./foobar`',
1311+
},
1312+
{
1313+
ruleId: 'order',
1314+
message: '`./bar` import should occur before import of `./foo`',
1315+
},
1316+
],
1317+
}),
1318+
// Bad alphabetical order with newlines-between: 'always-and-inside-groups' and missing newline
1319+
test({
1320+
code: `
1321+
var fs = require('fs');
1322+
var react = require('react');
1323+
1324+
var async = require('async');
1325+
`,
1326+
options: [
1327+
{
1328+
'newlines-between': 'always-and-inside-groups',
1329+
'sort': 'alphabetical',
1330+
},
1331+
],
1332+
errors: [
1333+
{
1334+
ruleId: 'order',
1335+
message: 'There should be at least one empty line between import groups',
1336+
},
1337+
{
1338+
ruleId: 'order',
1339+
message: '`async` import should occur before import of `react`',
1340+
},
1341+
],
1342+
}),
9941343
],
9951344
})

0 commit comments

Comments
 (0)