balena-supervisor/test/legacy/39-compose-commit.spec.ts
Felipe Lalanne 48e0733c7e Remove side effects for module imports
The supervisor uses the following pattern for async module
initialization

```typescript
// module.ts

export const initialised = (async () => {
    // do some async initialization
})();

// somewhere else
import * as module from 'module';

async function setup() {
  await module.initialise;
}
```

The above pattern means that whenever the module is imported, the
initialisation procedure will be ran, which is an anti-pattern.

This converts any instance of this pattern into a function

```typescript
export const initialised = _.once(async () => {
    // do some async initialization
});
```

And anywhere else on the code it replaces the call with a

```typescript
await module.initialised();
```

Change-type: patch
2022-09-06 15:48:18 -04:00

38 lines
1.1 KiB
TypeScript

import { expect } from 'chai';
import * as commitStore from '~/src/compose/commit';
import * as db from '~/src/db';
describe('compose/commit', () => {
before(async () => await db.initialized());
describe('Fetching commits', () => {
beforeEach(async () => {
// Clear the commit values in the db
await db.models('currentCommit').del();
});
it('should fetch a commit for an appId', async () => {
const commit = 'abcdef';
await commitStore.upsertCommitForApp(1, commit);
expect(await commitStore.getCommitForApp(1)).to.equal(commit);
});
it('should fetch the correct commit value when there is multiple commits', async () => {
const commit = 'abcdef';
await commitStore.upsertCommitForApp(1, '123456');
await commitStore.upsertCommitForApp(2, commit);
expect(await commitStore.getCommitForApp(2)).to.equal(commit);
});
});
it('should correctly insert a commit when a commit for the same app already exists', async () => {
const commit = 'abcdef';
await commitStore.upsertCommitForApp(1, '123456');
await commitStore.upsertCommitForApp(1, commit);
expect(await commitStore.getCommitForApp(1)).to.equal(commit);
});
});