Skip to content

Commit

Permalink
refactor: specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkeyl committed Jan 16, 2025
1 parent 49e6fdf commit b39a79c
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 118 deletions.
129 changes: 46 additions & 83 deletions specs/regex/bem-regex.spec.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,50 @@
import { BLOCK_REGEX, ELEMENT_REGEX, MODIFIER_REGEX } from '@archoleat/reglib';
import { describe, expect, test as spec } from 'bun:test';

import { CSSRules } from '#rules/css.ts';

describe('BEM Class Regex', async () => {
const BEMClassRegex = new RegExp(`${CSSRules['selector-class-pattern'][0]}`);

spec('should block', async () => {
expect('block').toMatch(BEMClassRegex);
});

spec('should block-name', async () => {
expect('block-name').toMatch(BEMClassRegex);
});

spec('should block-name-123', async () => {
expect('block-name-123').toMatch(BEMClassRegex);
});

spec('should block--modifier', async () => {
expect('block--modifier').toMatch(BEMClassRegex);
});

spec('should block-name--modifier-name', async () => {
expect('block-name--modifier-name').toMatch(BEMClassRegex);
});

spec('should block-name--modifier', async () => {
expect('block-name--modifier').toMatch(BEMClassRegex);
});

spec('should block--modifier-name', async () => {
expect('block--modifier-name').toMatch(BEMClassRegex);
});

spec('should block__element', async () => {
expect('block__element').toMatch(BEMClassRegex);
});

spec('should block-name__element-name', async () => {
expect('block-name__element-name').toMatch(BEMClassRegex);
});

spec('should block-name__element', async () => {
expect('block-name__element').toMatch(BEMClassRegex);
});

spec('should block__element-name', async () => {
expect('block__element-name').toMatch(BEMClassRegex);
});

spec('should block__element--modifier', async () => {
expect('block__element--modifier').toMatch(BEMClassRegex);
});

spec('should block-name__element-name--modifier-name', async () => {
expect('block-name__element-name--modifier-name').toMatch(BEMClassRegex);
});

spec('should block-name__element-name--modifier', async () => {
expect('block-name__element-name--modifier').toMatch(BEMClassRegex);
});

spec('should block-name__element--modifier-name', async () => {
expect('block-name__element--modifier-name').toMatch(BEMClassRegex);
});

spec('should block__element-name--modifier-name', async () => {
expect('block__element-name--modifier-name').toMatch(BEMClassRegex);
});

spec('should block__element_modifier', async () => {
expect('block__element_modifier').not.toMatch(BEMClassRegex);
});

spec('should Block', async () => {
expect('Block').not.toMatch(BEMClassRegex);
});

spec('should BlockElement', async () => {
expect('BlockElement').not.toMatch(BEMClassRegex);
});

spec('should 123', async () => {
expect('123').not.toMatch(BEMClassRegex);
describe('BEM Class Regex', () => {
const BEMClassRegex = new RegExp(
`^(?:${BLOCK_REGEX})(?:${ELEMENT_REGEX})?(?:${MODIFIER_REGEX})?$`,
);

const validTestCases = [
'block',
'block-name',
'block-name-123',
'block--modifier',
'block-name--modifier-name',
'block-name--modifier',
'block--modifier-name',
'block__element',
'block-name__element-name',
'block-name__element',
'block__element-name',
'block__element--modifier',
'block-name__element-name--modifier-name',
'block-name__element-name--modifier',
'block-name__element--modifier-name',
'block__element-name--modifier-name',
];

const invalidTestCases = [
'block__element_modifier',
'Block',
'BlockElement',
'123',
];

describe('should match valid BEM classes', () => {
validTestCases.forEach((testCase) => {
spec(`should match ${testCase}`, () => {
expect(testCase).toMatch(BEMClassRegex);
});
});
});

describe('should not match invalid BEM classes', () => {
invalidTestCases.forEach((testCase) => {
spec(`should not match ${testCase}`, () => {
expect(testCase).not.toMatch(BEMClassRegex);
});
});
});
});
55 changes: 40 additions & 15 deletions specs/utils/create-at-rule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,53 @@ import { beforeEach, describe, expect, test as spec } from 'bun:test';
import { createAtRule } from '#utils/create-at-rule/create-at-rule.ts';
import type { Parameters } from '#create-at-rule-parameters';

describe('Create At Rule', async () => {
let atRule: Function;
describe('Create At Rule', () => {
let atRule: (parameters: Parameters) => Promise<Parameters>;

beforeEach(async () => {
atRule = async (parameters: Parameters) => {
const { name, hasBlock } = parameters;

const result = await createAtRule({ name, hasBlock });

expect(result.name).toBeTypeOf('string');
expect(result.hasBlock).toBeTypeOf('boolean');
expect(result.type).toEqual('at-rule');

return result;
beforeEach(() => {
atRule = async ({ name, hasBlock }) => {
return createAtRule({ name, hasBlock });
};
});

spec('should create an object with a block', async () => {
await atRule({ name: 'test-rule-1', hasBlock: true });
const result = await atRule({ name: 'test-rule-1', hasBlock: true });

expect(result).toEqual({
name: 'test-rule-1',
hasBlock: true,
type: 'at-rule',
});
});

spec('should create an object without a block', async () => {
await atRule({ name: 'test-rule-2', hasBlock: false });
const result = await atRule({ name: 'test-rule-2', hasBlock: false });

expect(result).toEqual({
name: 'test-rule-2',
hasBlock: false,
type: 'at-rule',
});
});

const testCases = [
{
description: 'should handle empty name',
input: { name: '', hasBlock: true },
expected: { name: '', hasBlock: true, type: 'at-rule' },
},
{
description: 'should handle default hasBlock (false)',
input: { name: 'test-rule-3' },
expected: { name: 'test-rule-3', hasBlock: false, type: 'at-rule' },
},
];

testCases.forEach(({ description, input, expected }) => {
spec(description, async () => {
const result = await atRule(input);

expect(result).toEqual(expected);
});
});
});
48 changes: 42 additions & 6 deletions specs/utils/create-properties-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,55 @@ import { describe, expect, test as spec } from 'bun:test';

import { createPropertiesGroup } from '#utils/create-properties-group/create-properties-group.ts';

describe('Create Properties Group', async () => {
describe('Create Properties Group', () => {
spec(
'should create an object with (groupName: String, properties: [])',
async () => {
const properties = ['display', 'z-index'];
const groupName = 'Test Group Name';

await createPropertiesGroup({
groupName: 'Test Group Name',
const result = await createPropertiesGroup({ groupName, properties });

expect(result).toEqual({
groupName,
properties,
}).then((parameters) => {
expect(parameters.groupName).toEqual('Test Group Name');
expect(parameters.properties).toEqual(properties);
emptyLineBefore: 'threshold',
noEmptyLineBetween: true,
order: 'flexible',
});
},
);

const testCases = [
{
description: 'should handle empty properties array',
input: { groupName: 'Empty Properties Group', properties: [] },
expected: {
groupName: 'Empty Properties Group',
properties: [],
emptyLineBefore: 'threshold',
noEmptyLineBetween: true,
order: 'flexible',
},
},
{
description: 'should handle single property',
input: { groupName: 'Single Property Group', properties: ['display'] },
expected: {
groupName: 'Single Property Group',
properties: ['display'],
emptyLineBefore: 'threshold',
noEmptyLineBetween: true,
order: 'flexible',
},
},
];

testCases.forEach(({ description, input, expected }) => {
spec(description, async () => {
const result = await createPropertiesGroup(input);

expect(result).toEqual(expected);
});
});
});
60 changes: 47 additions & 13 deletions specs/utils/create-rule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,57 @@ import { beforeEach, describe, expect, test as spec } from 'bun:test';
import { createRule } from '#utils/create-rule/create-rule.ts';
import type { Parameters } from '#create-rule-parameters';

describe('Create Rule', async () => {
let rule: Function;
describe('Create Rule', () => {
let rule: (parameters: Parameters) => Promise<Parameters>;

beforeEach(async () => {
rule = async (parameters: Parameters) => {
const { selector } = parameters;

const result = await createRule({ selector });

// expect(result.selector).toEqual(`&${selector.replace(/^&+/g, '')}`);
expect(result.type).toEqual('rule');

return result;
beforeEach(() => {
rule = async (parameters) => {
return createRule(parameters);
};
});

spec('should create an object with selector', async () => {
await rule({ selector: ':any-nested-css-selector' });
const selector = ':any-nested-css-selector';
const result = await rule({ selector });

expect(result).toEqual({
type: 'rule',
selector: `&${selector}`,
});
});

const testCases = [
{
description: 'should handle selector starting with &',
input: { selector: '&:hover' },
expected: {
type: 'rule',
selector: '&:hover',
},
},
{
description: 'should handle selector with multiple &',
input: { selector: '&&&:focus' },
expected: {
type: 'rule',
selector: '&:focus',
},
},
{
description: 'should handle RegExp selector',
input: { selector: /\.class-name/ },
expected: {
type: 'rule',
selector: /\.class-name/,
},
},
];

testCases.forEach(({ description, input, expected }) => {
spec(description, async () => {
const result = await rule(input);

expect(result).toEqual(expected);
});
});
});
1 change: 1 addition & 0 deletions src/utils/create-at-rule/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type Parameters = {
name: string;
type?: string;
hasBlock?: boolean;
};

Expand Down
5 changes: 4 additions & 1 deletion src/utils/create-rule/create-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import type { Parameters } from './parameters.ts';
const createRule = async (parameters: Parameters) => {
const { selector } = parameters;

const normalizedSelector =
selector instanceof RegExp ? selector : `&${selector.replace(/^&+/g, '')}`;

return {
type: 'rule',
selector: selector instanceof RegExp ? selector : `&${selector}`,
selector: normalizedSelector,
};
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/create-rule/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type Parameters = {
selector: string | RegExp;
type?: string;
isAmpersand?: boolean;
};

Expand Down

0 comments on commit b39a79c

Please sign in to comment.