Skip to content

Commit

Permalink
test: 💍 Add test coverage with mocked GraphQL layer (initial) (#3)
Browse files Browse the repository at this point in the history
* chore: 🤖 initial test structure with msw

* chore: 🤖 start application tests

* test: 💍 add get applications

* test: 💍 add create application

* test: 💍 add update application

* test: 💍 add delete application

* test: 💍 add get domains

* chore: 🤖 delete unused, create alias

* chore: 🤖 update mocks

* chore: 🤖 update gitignore

* chore: 🤖 update applications test

* chore: 🤖 format

* chore: 🤖 update Domains handler

* test: 💍 extend domains test by id and hostname

* test: 💍 test domains by zone id

* test: 💍 create domain

* test: 💍 delete domain

* test: 💍 verify domain

* test: 💍 list zones

* test: 💍 get zone

* test: 💍 create zone for private gateway

* test: 💍 delete zone

* test: 💍 create ens record

* test: 💍 get ens record

* test: 💍 get ens record by name

* test: 💍 verify ens record

* test: 💍 delete ens record

* test: 💍 list ens records

* test: 💍 list by ipns record id

* test: 💍 get fleek functions by name

* test: 💍 get fleek functions

* test: 💍 create fleek functions

* test: 💍 delete fleek function

* test: 💍 add file to ipfs

* test: 💍 add files to ipfs

* test: 💍 todo should add files by path

* chore: 🤖 format

* fix: 🐛 conflict  amend

* refactor: 💡 generate cids texts

* chore: 🤖 remove graphql schema

* chore: 🤖 add TODO

* chore: 🤖 remove tester package
  • Loading branch information
heldrida authored Oct 25, 2024
1 parent 439e177 commit 4b6e28c
Show file tree
Hide file tree
Showing 30 changed files with 6,165 additions and 2,256 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ schema.graphql

# Local
.local

# Pnpm
.pnpm-store
27 changes: 27 additions & 0 deletions .scripts/generateCID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CID } from 'multiformats/cid';
import { sha256 } from 'multiformats/hashes/sha2';

async function textToCidV0(text: string): Promise<string> {
const textEncoder = new TextEncoder();
const bytes = textEncoder.encode(text);

const hash = await sha256.digest(bytes);

const cid = CID.createV0(hash);

return cid.toString();
}

const text = process.argv[2];

if (!text) {
console.error('👹 Oops! Please provide some text/content');
process.exit(1);
}

textToCidV0(text).then(cidString => {
console.log('✅ CIDv0:', cidString);
}).catch(error => {
console.error('👹 Oops! found error:', error);
process.exit(1);
});
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
"@biomejs/biome": "^1.8.3",
"@changesets/cli": "^2.27.6",
"@fleek-platform/errors": "^2.7.0",
"@fleek-platform/tester": "^2.11.0",
"@fleek-platform/utils-genql-client": "^0.1.1",
"@fleek-platform/utils-genql-client": "^0.2.1",
"@fleek-platform/utils-token": "^0.2.0",
"@jspm/core": "^2.0.1",
"@tsconfig/node16": "^16.1.3",
Expand All @@ -71,8 +70,10 @@
"esbuild": "^0.21.4",
"esbuild-plugin-polyfill-node": "^0.3.0",
"esbuild-plugins-node-modules-polyfill": "^1.6.4",
"graphql": "^16.9.0",
"import-meta-resolve": "^4.1.0",
"ipfs-http-client": "56.0.3",
"msw": "^2.5.0",
"multiformats": "^12.1.3",
"native-fetch": "^4.0.2",
"typescript": "5.6.2",
Expand Down
2,411 changes: 253 additions & 2,158 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

42 changes: 13 additions & 29 deletions src/FleekSdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
import { describe, expect } from 'vitest';
import { describe, expect, it, afterAll, afterEach, beforeAll } from 'vitest';
import { server } from './mocks/graphql/node';
import { mockGraphqlServiceApiUrl as graphqlServiceApiUrl } from './mocks/graphql/handlers';
import { FleekSdk } from './FleekSdk';

import { mockDatabasesAndGraphqlWithBrowserSdkForEachTest } from './testTools/mockDatabasesAndGraphqlWithBrowserSdkForEachTest';
import { mockDatabasesAndGraphqlWithNodeSdkForEachTest } from './testTools/mockDatabasesAndGraphqlWithNodeSdkForEachTest';

describe('[Node.js] getVersion', () => {
const { it } = mockDatabasesAndGraphqlWithNodeSdkForEachTest({
mockIpfs: false,
});

it('should get current version info', async (context) => {
const response = await context.sdks.josh.getVersion();

expect(response).toMatchInlineSnapshot(`
Object {
"version": Object {
"__typename": "Version",
"commitHash": "0fabad88415cedb2c3c21548afa14a949a088954",
},
}
`);
describe('FleekSDK', () => {
const sdk = new FleekSdk({
graphqlServiceApiUrl,
accessTokenService: {} as any,
});
});

describe('[Chromium] getVersion', () => {
const { it } = mockDatabasesAndGraphqlWithBrowserSdkForEachTest({
mockIpfs: false,
});
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

it('should get current version info', async (context) => {
const response = await context.sdks.josh({
callback: () => window.sdk.getVersion(),
});
it('should get current version info', async () => {
const response = await sdk.getVersion();

expect(response).toMatchInlineSnapshot(`
Object {
Expand Down
1 change: 1 addition & 0 deletions src/FleekSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class FleekSdk {

public getVersion = async () => {
const response = await this.graphqlClient.query({
__name: 'GetVersion',
version: { __scalar: true },
});

Expand Down
44 changes: 0 additions & 44 deletions src/clients/FleekSdk.test.ts

This file was deleted.

204 changes: 204 additions & 0 deletions src/clients/applications.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import { describe, expect, it, afterAll, afterEach, beforeAll } from 'vitest';
import { server } from '../mocks/graphql/node';
import { mockGraphqlServiceApiUrl as graphqlServiceApiUrl } from '../mocks/graphql/handlers';
import { FleekSdk } from '../FleekSdk';
import state from '../mocks/state';

describe('Applications', () => {
const sdk = new FleekSdk({
graphqlServiceApiUrl,
accessTokenService: {} as any,
});

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

it('should get application', async () => {
const response = await sdk.applications().get({
id: state.auth.application.electronicCoMobileApp.id,
});

expect(response).toMatchInlineSnapshot(`
Object {
"clientId": "client_SCmayempJ1d953yjn1yx",
"createdAt": "2023-03-23T12:05:13.641Z",
"id": "cli2ymypd000208l86gjd6p17",
"name": "electronicCoMobileApp",
"updatedAt": "2023-03-23T12:05:13.641Z",
"whiteLabelDomains": Array [
Object {
"hostname": "app.best-electronic.co",
"id": "clu2xf6uz000208jv6qskg1hm",
},
Object {
"hostname": "app.electronic.co",
"id": "clu2xd1bs000108jv0v0d2xmy",
},
],
"whitelistDomains": Array [
Object {
"hostname": "app.best-electronic.co",
"id": "cli2z1zim000008l66z4l7qg3",
},
Object {
"hostname": "app.electronic.co",
"id": "cli2z10wq000208jw42gd4pyh",
},
],
}
`);
});

it('should list applications', async () => {
const response = await sdk.applications().list();

expect(response).toMatchInlineSnapshot(`
Array [
Object {
"clientId": "client_ZRacrn3b1ForrjK5u8VD",
"createdAt": "2023-03-23T11:05:13.641Z",
"id": "cli2ymucu000108l81grqhzcp",
"name": "electronicCoWebApp",
"updatedAt": "2023-03-23T11:05:13.641Z",
"whiteLabelDomains": Array [],
"whitelistDomains": Array [],
},
Object {
"clientId": "client_SCmayempJ1d953yjn1yx",
"createdAt": "2023-03-23T12:05:13.641Z",
"id": "cli2ymypd000208l86gjd6p17",
"name": "electronicCoMobileApp",
"updatedAt": "2023-03-23T12:05:13.641Z",
"whiteLabelDomains": Array [
Object {
"hostname": "app.best-electronic.co",
"id": "clu2xf6uz000208jv6qskg1hm",
},
Object {
"hostname": "app.electronic.co",
"id": "clu2xd1bs000108jv0v0d2xmy",
},
],
"whitelistDomains": Array [
Object {
"hostname": "app.best-electronic.co",
"id": "cli2z1zim000008l66z4l7qg3",
},
Object {
"hostname": "app.electronic.co",
"id": "cli2z10wq000208jw42gd4pyh",
},
],
},
]
`);
});

it('should create application', async () => {
const response = await sdk.applications().create({
name: 'test-application',
whitelistDomains: ['fleek.xyz'],
});

// TODO: This mandates returning whitelistDomains
// and whiteLabelDomains, while the non-schema version
// does not requires it for some reason.
// Do a test in runtime to check
// see handler
expect(response).toMatchInlineSnapshot(
{
createdAt: expect.anything(),
id: expect.any(String),
updatedAt: expect.anything(),
},
`
Object {
"clientId": "client_testtesttest",
"createdAt": Anything,
"id": Any<String>,
"name": "test-application",
"updatedAt": Anything,
"whiteLabelDomains": Array [],
"whitelistDomains": Array [],
}
`,
);
});

it('should update application', async () => {
const response = await sdk.applications().update({
id: state.auth.application.electronicCoMobileApp.id,
name: 'new-mobile-app-name',
});

expect(response).toMatchInlineSnapshot(
{ updatedAt: expect.anything() },
`
Object {
"clientId": "client_SCmayempJ1d953yjn1yx",
"createdAt": "2023-03-23T12:05:13.641Z",
"id": "cli2ymypd000208l86gjd6p17",
"name": "new-mobile-app-name",
"updatedAt": Anything,
"whiteLabelDomains": Array [
Object {
"hostname": "app.best-electronic.co",
"id": "clu2xf6uz000208jv6qskg1hm",
},
Object {
"hostname": "app.electronic.co",
"id": "clu2xd1bs000108jv0v0d2xmy",
},
],
"whitelistDomains": Array [
Object {
"hostname": "app.best-electronic.co",
"id": "cli2z1zim000008l66z4l7qg3",
},
Object {
"hostname": "app.electronic.co",
"id": "cli2z10wq000208jw42gd4pyh",
},
],
}
`,
);
});

it('should delete application', async () => {
const response = await sdk.applications().delete({
id: state.auth.application.electronicCoMobileApp.id,
});

expect(response).toMatchInlineSnapshot(`
Object {
"clientId": "client_SCmayempJ1d953yjn1yx",
"createdAt": "2023-03-23T12:05:13.641Z",
"id": "cli2ymypd000208l86gjd6p17",
"name": "electronicCoMobileApp",
"updatedAt": "2023-03-23T12:05:13.641Z",
"whiteLabelDomains": Array [
Object {
"hostname": "app.best-electronic.co",
"id": "clu2xf6uz000208jv6qskg1hm",
},
Object {
"hostname": "app.electronic.co",
"id": "clu2xd1bs000108jv0v0d2xmy",
},
],
"whitelistDomains": Array [
Object {
"hostname": "app.best-electronic.co",
"id": "cli2z1zim000008l66z4l7qg3",
},
Object {
"hostname": "app.electronic.co",
"id": "cli2z10wq000208jw42gd4pyh",
},
],
}
`);
});
});
Loading

0 comments on commit 4b6e28c

Please sign in to comment.