Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests, bug fix in generateRandomString #23

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
'@storybook/preset-create-react-app',
'@storybook/addon-a11y',
'storybook-addon-designs',
'@storybook/addon-storyshots',
],
features: {
interactionDebugger: true,
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ npm run build-storybook

Once the app is built, it is ready to be deployed.

To run tests, use the following command:
To run storybook tests, use the following command:

```bash
npm run test-storybook
```

To run unit tests, use the following command:

```bash
npm run test
```

To get the test coverage, use the following command:

```bash
Expand Down
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"peerDependencies": {
"react": "^18.2.0",
"ts-jest": "^29.1.0",
"react-dom": "^18.2.0"
},
"scripts": {
Expand All @@ -26,7 +27,8 @@
"test-storybook": "test-storybook",
"chromatic": "chromatic --exit-zero-on-changes",
"prepare": "husky install",
"build": "rollup -c"
"build": "rollup -c",
"test": "jest"
},
"eslintConfig": {
"extends": [
Expand Down
4 changes: 1 addition & 3 deletions src/shared/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export const generateRandomString = (length: number) =>
Math.random()
.toString(36)
.substring(2, length + 2);
Array.from({ length: length }, () => Math.random().toString(36)[2]).join('');
75 changes: 75 additions & 0 deletions tests/styles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { spacing, convertToSpacingUnit, pxToRem } from '../src/shared/styles';

describe('testing spacing', () => {
test('No value given', () => {
expect(spacing()).toBe('0');
});

test('Empty array given', () => {
expect(spacing([])).toBe('');
});

test('Very big spacing', () => {
expect(spacing(500000)).toBe('250000rem');
});

test('Zero spacing', () => {
expect(spacing(0)).toBe('0');
});

test('Negative value', () => {
expect(spacing(-1)).toBe('-0.5rem');
});

test('Oversize array to maximum size of 4', () => {
expect(spacing([1, 2, 3, 4, 5])).toBe('0.5rem 1rem 1.5rem 2rem');
});

test('Array of 0,1,1,0', () => {
expect(spacing([0, 1, 1, 0])).toBe('0rem 0.5rem 0.5rem 0rem');
});
});

describe('testing convertToSpacingUnit', () => {
test('No value given', () => {
expect(convertToSpacingUnit()).toBe(0);
});

test('Zero value', () => {
expect(convertToSpacingUnit(0)).toBe(0);
});

test('Positive value', () => {
expect(convertToSpacingUnit(80)).toBe(10);
});

test('Negative value', () => {
expect(convertToSpacingUnit(-80)).toBe(-10);
});

test('Very large value', () => {
expect(convertToSpacingUnit(8000000)).toBe(1000000);
});
});

describe('testing pxToRem', () => {
test('No value given', () => {
expect(pxToRem()).toBe('0');
});

test('Zero value', () => {
expect(pxToRem(0)).toBe('0');
});

test('Positive value', () => {
expect(pxToRem(160)).toBe('10rem');
});

test('Negative value', () => {
expect(pxToRem(-160)).toBe('-10rem');
});

test('Very large value', () => {
expect(pxToRem(16000000)).toBe('1000000rem');
});
});
21 changes: 21 additions & 0 deletions tests/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { generateRandomString } from '../src/shared/util';

describe('testing generateRandomString', () => {
test('Various lengths', () => {
for (let i = 0; i <= 100; i++) {
const str = generateRandomString(i);
expect(str.length).toBe(i);
}
});

test('Always correct length and only valid characters', () => {
for (let i = 0; i < 1000; i++) {
const str = generateRandomString(10);
expect(str.length).toBe(10);

const matched = str.match(/[^a-z]|[^0-9]/g); // a-z and 0-9
const validCharacterCount = (matched || '').length;
expect(validCharacterCount).toBe(10);
}
});
});