Skip to content
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
18 changes: 17 additions & 1 deletion packages/random-name/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

A tiny utility to generate random names

Fully inspired by [Moby name generator](https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go)

---

## Install

```bash
$ yarn add @scaleway/random-names
$ yarn add @scaleway/random-name
```

## Usage
Expand All @@ -22,3 +24,17 @@ randomName('superb') // => "superb-admiring-allen"
randomName('superb', '_') // => "superb_admiring_allen"
randomName('', '_') // => "admiring_allen"
```

As a React hook
```js
import React from 'react'
import { useRandomName } from '@scaleway/random-name'

const Component = () => {
const name = useRandomName()

return (
<span>{name}</span>
)
}
```
8 changes: 6 additions & 2 deletions packages/random-name/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
"directory": "packages/random-name"
},
"license": "MIT",
"dependencies": {
"docker-names": "^1.1.1"
"devDependencies": {
"@testing-library/react-hooks": "^7.0.1",
"react": "^17.0.2"
},
"peerDependencies": {
"react": ">=16.8"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`randomName useRandomName useTranslation should not be defined without I18nProvider 1`] = `"gracious-hermann"`;
25 changes: 21 additions & 4 deletions packages/random-name/src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import randomName from '..'
import { renderHook } from '@testing-library/react-hooks'
import randomName, { useRandomName } from '..'

describe('randomNames', () => {
describe('randomName', () => {
it('should return a random name separated by a dash', () => {
expect(randomName().split('-').length).toBe(2)
})
Expand All @@ -21,10 +22,26 @@ describe('randomNames', () => {
expect(name.split('!').length).toBe(3)
})

it('should never includes the word "cocks"', () => {
it('should never have boring-wozniak', () => {
const names = Array.from(Array(1000000), () => randomName())
expect(names).not.toEqual(
expect.arrayContaining([expect.stringMatching('cocks')]),
expect.arrayContaining([expect.stringMatching('boring-wozniak')]),
)
})

describe('useRandomName', () => {
beforeAll(() => {
jest.spyOn(global.Math, 'random').mockReturnValue(0.4155913669444804)
})

afterAll(() => {
jest.spyOn(global.Math, 'random').mockRestore()
})

it('useTranslation should not be defined without I18nProvider', () => {
const { result } = renderHook(() => useRandomName())
expect(result.current).toMatchSnapshot()
})
})

})
Loading