Skip to content

Commit

Permalink
chore: Improve mocks readme. Improve destructuring syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
yusinto committed Jan 24, 2024
1 parent b8a6f7b commit 8e6a25e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
35 changes: 29 additions & 6 deletions packages/shared/mocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,51 @@ module.exports = {
## Usage
> [!IMPORTANT]
> basicPlatform must be used inside a test because it's setup before each test.
> basicPlatform and clientContext must be used inside a test because it's setup before each test.
- `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.
- `clientContext`: ClientContext object including `basicPlatform` above. This is setup beforeEach so it must be used inside a test as well.
- `hasher`: a Hasher object returned by `Crypto.createHash`. All functions in this object are jest mocks. This is exported
separately as a top level export because `Crypto` does not expose this publicly and we want to respect that.
## Example
```tsx
import { basicPlatform, hasher } from '@launchdarkly/private-js-mocks';
import { basicPlatform, clientContext, hasher } from '@launchdarkly/private-js-mocks';
// DOES NOT WORK: crypto is undefined because basicPlatform must be inside a test
// because it's setup by the package in beforeEach.
const { crypto } = basicPlatform; // DON'T DO THIS
const { crypto } = basicPlatform; // DON'T DO THIS HERE
// DOES NOT WORK: clientContext must be used inside a test. Otherwise all properties
// of it will be undefined.
const {
basicConfiguration: { serviceEndpoints, tags },
platform: { info },
} = clientContext; // DON'T DO THIS HERE
describe('button', () => {
// DOES NOT WORK: again must be inside an actual test. At the test suite,
// level, beforeEach has not been run.
const { crypto } = basicPlatform; // DON'T DO THIS
const { crypto } = basicPlatform; // DON'T DO THIS HERE
// DO THIS
let crypto: Crypto;
let info: Info;
let serviceEndpoints: ServiceEndpoints;
let tags: ApplicationTags;
beforeEach(() => {
// WORKS: basicPlatform has been setup by the package.
crypto = basicPlatform.crypto; // DO THIS
// WORKS: basicPlatform and clientContext have been setup by the package.
({ crypto, info } = basicPlatform);
// WORKS
({
basicConfiguration: { serviceEndpoints, tags },
platform: { info },
} = clientContext);
});
afterEach(() => {
Expand All @@ -71,8 +89,13 @@ describe('button', () => {
const [bucket, hadContext] = bucketer.bucket();
// assert
// WORKS
expect(crypto.createHash).toHaveBeenCalled();
// WORKS: alternatively you can just use the full path to access the properties
// of basicPlatform
expect(basicPlatform.crypto.createHash).toHaveBeenCalled();
// GOTCHA: hasher is a separte import from crypto to respect
// the public Crypto interface.
expect(hasher.update).toHaveBeenCalledWith(expected);
Expand Down
4 changes: 1 addition & 3 deletions packages/shared/sdk-client/src/utils/addAutoEnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ describe('addAutoEnv', () => {
let info: Info;

beforeEach(() => {
crypto = basicPlatform.crypto;
info = basicPlatform.info;

({ crypto, info } = basicPlatform);
(crypto.randomUUID as jest.Mock).mockResolvedValue('test-device-key-1');
});

Expand Down

0 comments on commit 8e6a25e

Please sign in to comment.