Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
move tests/ to tests-old/ and exclude from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Sep 12, 2023
1 parent 06247de commit 67cc1bb
Show file tree
Hide file tree
Showing 29 changed files with 195 additions and 1 deletion.
104 changes: 104 additions & 0 deletions tests-old/unit/plugins/urql.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {
createClient,
dedupExchange,
cacheExchange,
fetchExchange,
makeOperation,
} from "@urql/core";
import { authExchange } from "@urql/exchange-auth";
import { pipe, fromValue, fromArray, subscribe } from "wonka";
import { gql } from "graphql-tag";

describe("one", () => {
it("wonka test", async () => {
pipe(
fromArray([1, 2, 3]),
subscribe((x) => console.log(x))
);
});

it("one", async () => {
const url = "http://localhost:5001/graphql";

const getAuth = async ({ authState }) => {
if (!authState) {
const token = "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";
// const token = "HV41CNl97jxwbDtQ";
return { token };
}
return null;
};

const addAuthToOperation = ({ authState, operation }) => {
if (!authState || !authState.token) {
return operation;
}

const fetchOptions =
typeof operation.context.fetchOptions === "function"
? operation.context.fetchOptions()
: operation.context.fetchOptions || {};

return makeOperation(operation.kind, operation, {
...operation.context,
fetchOptions: {
...fetchOptions,
headers: {
...fetchOptions.headers,
Authorization: `Bearer "${authState.token}"`,
},
},
});
};

const client = createClient({
url: url,
exchanges: [
// dedupExchange,
// cacheExchange, // default document cache
authExchange({ getAuth, addAuthToOperation }),
fetchExchange,
],
});

// const executeQuery = () =>
// fromValue({
// data: { foo: "bar" },
// });

const executeQuery = vi.fn().mockReturnValue(
fromValue({
data: { foo: "bar" },
})
);

// @ts-ignore
// client.executeQuery = executeQuery;

console.log("subscribeToDebugTarget: ", client.subscribeToDebugTarget);
const { unsubscribe } =
client.subscribeToDebugTarget?.((event) => {
// console.log(event);
console.log(JSON.stringify(event, null, 2));
}) || {};

const spy = vi.spyOn(client, "query");

const r = await client
.query(
gql`
{
version
}
`,
{}
)
.toPromise();
// console.log(r);
// console.log(executeQuery.mock.calls);
// console.log(JSON.stringify(executeQuery.mock.calls, null, 2));
// console.log(spy.mock.calls);

unsubscribe?.();
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions tests-old/unit/stores/scratch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { setActivePinia, createPinia } from "pinia";
import { defineStore } from "pinia";

console.log("before useAStore()");
const useAStore = defineStore("A", () => {
console.log("in defineStore()");
return { a: 1 };
});
// const useAStore = (() => {
// console.log("in useAStore()");
// return defineStore("A", () => {
// console.log("in defineStore()");
// return { a: 1 };
// });
// })();
console.log("after useAStore()");

describe("scratch", () => {
beforeEach(() => {
setActivePinia(createPinia());
});

it("one", () => {
console.log("in one()");
const aStore = useAStore();
console.log("end one()");
});
});
62 changes: 62 additions & 0 deletions tests-old/unit/utils/query-state.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { defineComponent, ref, Ref } from "vue";
import { mount, createLocalVue } from "@vue/test-utils";
import { PiniaVuePlugin } from "pinia";
import { useQuery, CombinedError } from "@urql/vue";
import { createTestingPinia } from "@pinia/testing";
import gql from "graphql-tag";
import { fromValue, never } from "wonka";

import { useStore } from "@/plugins/pinia/stores/main";
import { useQueryState } from "@/utils/query-state";

export const FooDocument = gql`
query foo {
foo
}
`;

export type FooQuery = {
__typename?: "Query";
foo?: string | null;
};

describe("one", () => {
let localVue: ReturnType<typeof createLocalVue>;
let store: ReturnType<typeof useStore>;

function createQueryResponse(client: Ref<{ executeQuery: () => unknown }>) {
// queryResponse is the return value of useQuery, which needs to be called
// inside of a component setup(). The useQuery() injects the urql client
// with the key "$urql".
let ret: ReturnType<typeof useQuery<FooQuery>> | undefined;
const dummyComponent = defineComponent({
setup() {
ret = useQuery<FooQuery>({ query: FooDocument });
},
render() {},
});
const pinia = createTestingPinia();
store = useStore(pinia);
mount(dummyComponent, {
localVue,
pinia,
provide: { $urql: client },
});
if (!ret) throw new Error("query is undefined");
return ret;
}

beforeEach(() => {
localVue = createLocalVue();
localVue.use(PiniaVuePlugin);
})

it("loading", () => {
const executeQuery = () => never; // fetching
const client = ref({ executeQuery });
const query = createQueryResponse(client);
expect(query.fetching.value).toBe(true);
const queryState = useQueryState(query);
// console.log(queryState);
});
});
File renamed without changes.
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default mergeConfig(
defineConfig({
test: {
environment: "jsdom",
exclude: [...configDefaults.exclude, "e2e/*"],
exclude: [...configDefaults.exclude, "e2e/*", "tests-old/**/*"],
root: fileURLToPath(new URL("./", import.meta.url)),
},
})
Expand Down

0 comments on commit 67cc1bb

Please sign in to comment.