|
2 | 2 |
|
3 | 3 | [![Actions Status][mocks-ci-badge]][mocks-ci] |
4 | 4 |
|
5 | | -**Internal use only.** |
| 5 | +> [!CAUTION] |
| 6 | +> Internal use only. |
| 7 | +> This project contains JavaScript mocks that are consumed in unit tests in client-side and server-side JavaScript SDKs. |
6 | 8 |
|
7 | | -This project contains JavaScript mocks that are consumed in unit tests in client-side and server-side JavaScript SDKs. |
| 9 | +## Installation |
| 10 | + |
| 11 | +This package is not published publicly. To use it internally, add the following line to your project's package.json |
| 12 | +devDependencies. yarn workspace has been setup to recognize this package so this dependency should automatically work: |
| 13 | + |
| 14 | +```bash |
| 15 | + "devDependencies": { |
| 16 | + "@launchdarkly/private-js-mocks": "0.0.1", |
| 17 | + ... |
| 18 | +``` |
| 19 | +
|
| 20 | +Then in your jest config add `@launchdarkly/private-js-mocks/setup` to setupFilesAfterEnv: |
| 21 | +
|
| 22 | +```js |
| 23 | +// jest.config.js or jest.config.json |
| 24 | +module.exports = { |
| 25 | + setupFilesAfterEnv: ['@launchdarkly/private-js-mocks/setup'], |
| 26 | + ... |
| 27 | +} |
| 28 | +``` |
| 29 | +
|
| 30 | +## Usage |
| 31 | +
|
| 32 | +> [!IMPORTANT] |
| 33 | +> basicPlatform and clientContext must be used inside a test because it's setup before each test. |
| 34 | +
|
| 35 | +- `basicPlatform`: a concrete but basic implementation of [Platform](https://github.com/launchdarkly/js-core/blob/main/packages/shared/common/src/api/platform/Platform.ts). This is setup beforeEach so it must be used inside a test. |
| 36 | +
|
| 37 | +- `clientContext`: ClientContext object including `basicPlatform` above. This is setup beforeEach so it must be used inside a test as well. |
| 38 | +
|
| 39 | +- `hasher`: a Hasher object returned by `Crypto.createHash`. All functions in this object are jest mocks. This is exported |
| 40 | + separately as a top level export because `Crypto` does not expose this publicly and we want to respect that. |
| 41 | +
|
| 42 | +## Example |
| 43 | +
|
| 44 | +```tsx |
| 45 | +import { basicPlatform, clientContext, hasher } from '@launchdarkly/private-js-mocks'; |
| 46 | +
|
| 47 | +// DOES NOT WORK: crypto is undefined because basicPlatform must be inside a test |
| 48 | +// because it's setup by the package in beforeEach. |
| 49 | +const { crypto } = basicPlatform; // DON'T DO THIS HERE |
| 50 | +
|
| 51 | +// DOES NOT WORK: clientContext must be used inside a test. Otherwise all properties |
| 52 | +// of it will be undefined. |
| 53 | +const { |
| 54 | + basicConfiguration: { serviceEndpoints, tags }, |
| 55 | + platform: { info }, |
| 56 | +} = clientContext; // DON'T DO THIS HERE |
| 57 | +
|
| 58 | +describe('button', () => { |
| 59 | + // DOES NOT WORK: again must be inside an actual test. At the test suite, |
| 60 | + // level, beforeEach has not been run. |
| 61 | + const { crypto } = basicPlatform; // DON'T DO THIS HERE |
| 62 | +
|
| 63 | + // DO THIS |
| 64 | + let crypto: Crypto; |
| 65 | + let info: Info; |
| 66 | + let serviceEndpoints: ServiceEndpoints; |
| 67 | + let tags: ApplicationTags; |
| 68 | +
|
| 69 | + beforeEach(() => { |
| 70 | + // WORKS: basicPlatform and clientContext have been setup by the package. |
| 71 | + ({ crypto, info } = basicPlatform); |
| 72 | +
|
| 73 | + // WORKS |
| 74 | + ({ |
| 75 | + basicConfiguration: { serviceEndpoints, tags }, |
| 76 | + platform: { info }, |
| 77 | + } = clientContext); |
| 78 | + }); |
| 79 | +
|
| 80 | + afterEach(() => { |
| 81 | + jest.resetAllMocks(); |
| 82 | + }); |
| 83 | +
|
| 84 | + it('hashes the correct string', () => { |
| 85 | + // arrange |
| 86 | + const bucketer = new Bucketer(crypto); |
| 87 | +
|
| 88 | + // act |
| 89 | + const [bucket, hadContext] = bucketer.bucket(); |
| 90 | +
|
| 91 | + // assert |
| 92 | + // WORKS |
| 93 | + expect(crypto.createHash).toHaveBeenCalled(); |
| 94 | +
|
| 95 | + // WORKS: alternatively you can just use the full path to access the properties |
| 96 | + // of basicPlatform |
| 97 | + expect(basicPlatform.crypto.createHash).toHaveBeenCalled(); |
| 98 | +
|
| 99 | + // GOTCHA: hasher is a separte import from crypto to respect |
| 100 | + // the public Crypto interface. |
| 101 | + expect(hasher.update).toHaveBeenCalledWith(expected); |
| 102 | + expect(hasher.digest).toHaveBeenCalledWith('hex'); |
| 103 | + }); |
| 104 | +}); |
| 105 | +``` |
| 106 | +
|
| 107 | +## Developing this package |
| 108 | +
|
| 109 | +If you make changes to this package, you'll need to run `yarn build` in the `mocks` directory for changes to take effect. |
8 | 110 |
|
9 | 111 | ## Contributing |
10 | 112 |
|
|
0 commit comments