Skip to content

Commit

Permalink
Merge pull request #4 from flakeed/main
Browse files Browse the repository at this point in the history
Fix tests for the package
  • Loading branch information
FreePhoenix888 committed Jun 3, 2023
2 parents 30b6935 + 9f0fecd commit 1ae3bcb
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 44 deletions.
49 changes: 22 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
},
"peerDependencies": {
"@deep-foundation/deeplinks": "^0.0.189"
},
"dependencies": {
"@apollo/client": "^3.7.15"
}
}
31 changes: 14 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
DeepClient,
UseDeepSubscriptionResult,
useDeepSubscription,
} from '@deep-foundation/deeplinks/imports/client.js';
import { useMemo } from 'react';
} from '@deep-foundation/deeplinks/imports/client';
import { useState, useEffect, useMemo, useRef } from 'react';

export function useArePackagesInstalled(param: UseArePackagesInstalledParam) {
const { packageNames, deep } = param;
const { packageNames } = param;

const { data, loading, error } = deep.useDeepSubscription({
const { data, loading, error } = useDeepSubscription({
type_id: {
_id: ['@deep-foundation/core', 'Package'],
},
Expand All @@ -18,28 +18,25 @@ export function useArePackagesInstalled(param: UseArePackagesInstalledParam) {
},
});

const packageInstallationStatuses: PackageInstallationStatuses | undefined = useMemo(() => {
if (!data) {
return undefined;
} else {
return packageNames.reduce<PackageInstallationStatuses>(
(acc, packageName) => {
acc[packageName] = !!data.find(
let packageInstallationStatuses: PackageInstallationStatuses = undefined;
if (data) {
packageInstallationStatuses =
packageNames.reduce<PackageInstallationStatuses>(
(packageInstallationStatuses, packageName) => {
packageInstallationStatuses![packageName] = !!data.find(
(item) => item.value?.value === packageName
);
return acc;
return packageInstallationStatuses;
},
{} as PackageInstallationStatuses
);
}
}, [data]);
}

return { packageInstallationStatuses, loading, error };
}

export interface UseArePackagesInstalledParam {
deep: DeepClient;
packageNames: Array<string>;
}

export type PackageInstallationStatuses = Record<string, boolean>;
export type PackageInstallationStatuses = Record<string, boolean> | undefined;
74 changes: 74 additions & 0 deletions tests/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import dotenv from 'dotenv';
import { DeepClient, DeepProvider } from '@deep-foundation/deeplinks/imports/client.js';
import { generateApolloClient } from '@deep-foundation/hasura/client.js';
import { render, waitFor } from '@testing-library/react';
import { useArePackagesInstalled } from '../src/main';
import { ApolloProvider } from '@apollo/client/index.js';
import { assert } from 'chai';
import React from 'react';
dotenv.config();

const requiredEnvNames = ['GRAPHQL_PATH', 'TOKEN', 'PACKAGE_NAMES'];

requiredEnvNames.forEach((name) => {
if (!process.env[name]) {
throw new Error(`Missing required environment variable: ${name}`);
}
});

const path = process.env.GRAPHQL_PATH!;
console.log("graphQlPath", path)

const token = process.env.TOKEN!;
console.log("token", token)

const packageNames = process.env.PACKAGE_NAMES!.split(',');
const timeout = process.env.TIMEOUT ? parseInt(process.env.TIMEOUT) : 10000;

const apolloClient = generateApolloClient({
path,
ssl: true,
ws: true,
token
});
console.log("apolloclient", apolloClient)

describe('main', () => {
it('installed packages', async () => {

const deep = new DeepClient({ apolloClient });
await deep.whoami();

console.log("deep.linkid", deep.linkId)

assert.notEqual(deep.linkId, 0);
assert.notEqual(deep.linkId, undefined);

let testResult;
const TestComponent = () => {
testResult = useArePackagesInstalled({ packageNames });
console.log("testresult", testResult)
return null;
};

render(
<ApolloProvider client={deep.apolloClient}>
<DeepProvider>
<TestComponent />
</DeepProvider>
</ApolloProvider>
);

await waitFor(
() => {
expect(testResult.loading).toBe(false);
expect(testResult.error).toBe(undefined);
expect(testResult.packageInstallationStatuses).not.toBe(undefined);
expect(Object.keys(testResult.packageInstallationStatuses)).toEqual(packageNames);
},
{
timeout,
}
);
});
});

0 comments on commit 1ae3bcb

Please sign in to comment.