Skip to content

Commit

Permalink
chore(deps): upgrade jest for prettier v3
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Jun 15, 2024
1 parent aefc1af commit dcd6ac2
Show file tree
Hide file tree
Showing 9 changed files with 850 additions and 118 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-pears-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gqty': patch
---

Upgrade jest for Prettier v3
2 changes: 1 addition & 1 deletion packages/gqty/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"graphql": "^16.8.2",
"graphql-sse": "^2.5.3",
"graphql-ws": "^5.16.0",
"jest": "^29.7.0",
"jest": "^30.0.0-alpha.5",
"just-memoize": "^2.2.0",
"open-cli": "^8.0.0",
"p-lazy": "^3.1.0",
Expand Down
10 changes: 6 additions & 4 deletions packages/gqty/src/Accessor/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,13 @@ const arrayProxyHandler: ProxyHandler<CacheObject[]> = {
};

export const createArrayAccessor = <
TSchemaType extends GeneratedSchemaObject[]
TSchemaType extends GeneratedSchemaObject[],
>(
meta: Meta
) => {
if (!Array.isArray(meta.cache.data)) {
const { cache } = meta;

if (!Array.isArray(cache.data)) {
if (verbose) {
console.warn(
'Invalid cache for an array accessor, monkey-patch by wrapping it with an array.',
Expand All @@ -543,10 +545,10 @@ export const createArrayAccessor = <
);
}

meta.cache.data = [meta.cache.data];
cache.data = [cache.data];
}

const proxy = new Proxy(meta.cache.data as TSchemaType, arrayProxyHandler);
const proxy = new Proxy(cache.data as TSchemaType, arrayProxyHandler);

$meta.set(proxy, meta);

Expand Down
30 changes: 17 additions & 13 deletions packages/gqty/test/accessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('setCache', () => {
unsubscribe();

expect(query.hello).toBe('12345');
expect(mockedFn).toBeCalledTimes(1);
expect(mockedFn).toHaveBeenCalledTimes(1);
expect(selections).toMatchInlineSnapshot(`
[
[
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('setCache', () => {

unsubscribe();

expect(mockedFn).toBeCalledTimes(1);
expect(mockedFn).toHaveBeenCalledTimes(1);
expect(selections).toMatchInlineSnapshot(`
[
[
Expand All @@ -274,28 +274,32 @@ describe('setCache', () => {
expect(() => {
// @ts-expect-error
setCache((_args?: { a: string }) => {}, undefined);
}).toThrowError('Subject must be an accessor.');
}).toThrow(new Error('Subject must be an accessor.'));

expect(() => {
setCache(query, (() => {}) as any);
}).toThrowError(
'Data must be a subset of the schema object, got type: ' + 'function'
}).toThrow(
new Error(
'Data must be a subset of the schema object, got type: ' + 'function.'
)
);

expect(() => {
setCache(query, 123123 as any);
}).toThrowError(
'Data must be a subset of the schema object, got type: ' + 'number'
}).toThrow(
new Error(
'Data must be a subset of the schema object, got type: ' + 'number.'
)
);

expect(() => {
setCache({}, {});
}).toThrowError('Subject must be an accessor.');
}).toThrow(new Error('Subject must be an accessor.'));

expect(() => {
// @ts-expect-error
query.human({ name: 'ñññ' }).sons['hello'] = null;
}).toThrowError('Invalid array assignment.');
}).toThrow(new Error('Invalid array assignment.'));
});
});

Expand Down Expand Up @@ -360,7 +364,7 @@ describe('assign selections', () => {

assignSelections(human, human);

expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);

const prevNodeEnv = process.env.NODE_ENV;
try {
Expand All @@ -371,7 +375,7 @@ describe('assign selections', () => {
process.env.NODE_ENV = prevNodeEnv;
}

expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);

spy.mockRestore();
});
Expand All @@ -388,11 +392,11 @@ describe('assign selections', () => {

expect(() => {
assignSelections({}, {});
}).toThrowError('Invalid source proxy');
}).toThrow(new Error('Invalid source proxy'));

expect(() => {
assignSelections(query, {} as any);
}).toThrowError('Invalid target proxy');
}).toThrow(new Error('Invalid target proxy'));
});
});

Expand Down
47 changes: 4 additions & 43 deletions packages/gqty/test/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { assertIsDefined } from 'test-utils';
import { $meta, createSchemaAccessor } from '../src/Accessor';
import { createObjectAccessor } from '../src/Accessor/resolve';
import { Cache, CacheRoot } from '../src/Cache';
import { createSchemaAccessor } from '../src/Accessor';
import { Cache, type CacheRoot } from '../src/Cache';
import { Selection } from '../src/Selection';

describe('Cache#dataAccessors', () => {
Expand All @@ -28,43 +26,6 @@ describe('Cache#dataAccessors', () => {
hasCacheMiss: false,
} as const;

/**
* Before we figure out how to cache sub-selections for null values, caching
* accessors adds a lot of complixity with little performance gain.
*
* Skip this for now.
*/
it('should return the cached accessor', () => {
const cache = new Cache();
const selection = Selection.createRoot('query').getChild('a');
const cacheValue = { a: 1 };

const obj = cache.getAccessor(selection, () =>
createObjectAccessor({
cache: {
data: cacheValue,
expiresAt: Infinity,
swrBefore: 0,
},
context: { ...mockContext, cache },
selection,
type: { __type: 'query' },
})
);

expect($meta(obj)).toBeDefined();
expect($meta(obj)?.cache.data).toEqual({ a: 1 });

const obj2 = cache.getAccessor(selection, () => {
throw Error("This shouldn't be called");
});

expect(obj).toBe(obj2);
assertIsDefined(selection);
expect(selection.key).toBe('a');
expect(selection.cacheKeys).toEqual(['query', 'a']);
});

it('should make selections', () => {
const cache = new Cache();
const schema = createSchemaAccessor<{ query: { b: { c: string } } }>({
Expand Down Expand Up @@ -463,7 +424,7 @@ describe('Cache#subscribe', () => {
},
});

expect(listener).toBeCalledTimes(1);
expect(listener).toHaveBeenCalledTimes(1);
expect(cache).toMatchInlineSnapshot(`
{
"query": {
Expand Down Expand Up @@ -508,7 +469,7 @@ describe('Cache#subscribe', () => {
},
});

expect(listener).toBeCalledTimes(1);
expect(listener).toHaveBeenCalledTimes(1);
expect(listener).toHaveBeenCalledWith({
query: {
c: { __typename: 'B', id: 2, b: 4 },
Expand Down
6 changes: 3 additions & 3 deletions packages/gqty/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('core#resolve', () => {

await expect(() =>
resolve(({ query }) => query.hello, { cachePolicy: 'only-if-cached' })
).rejects.toThrowError(new TypeError('Failed to fetch'));
).rejects.toThrow(new TypeError('Failed to fetch'));

await expect(resolve(({ query }) => query.hello)).resolves.toBe(
'hello world'
Expand Down Expand Up @@ -268,7 +268,7 @@ describe('compat', () => {
resolved(() => query.hello, { refetch: true, onCacheData })
).resolves.toBe('hello world');

expect(onCacheData).toBeCalledTimes(1);
expect(onCacheData).toHaveBeenCalledTimes(1);

const onCacheData2 = jest
.fn()
Expand All @@ -282,7 +282,7 @@ describe('compat', () => {
resolved(() => query.hello, { refetch: true, onCacheData: onCacheData2 })
).resolves.toBe('hello world');

expect(onCacheData2).toBeCalledTimes(1);
expect(onCacheData2).toHaveBeenCalledTimes(1);
});

test('resolved with operationName', async () => {
Expand Down
19 changes: 9 additions & 10 deletions packages/gqty/test/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ describe('refetch function', () => {
});

expect(value).toBe(123);
expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);

process.env.NODE_ENV = 'production';

Expand All @@ -1165,7 +1165,7 @@ describe('refetch function', () => {
});

expect(value2).toBe(456);
expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);
} finally {
process.env.NODE_ENV = prevEnv;
spy.mockRestore();
Expand Down Expand Up @@ -1247,14 +1247,14 @@ describe('refetch function', () => {
const invalidProxy = {};
const refetchData = await refetch(invalidProxy);

expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);
expect(refetchData).toBe(invalidProxy);

process.env.NODE_ENV = 'production';

const refetchData2 = await refetch(invalidProxy);

expect(spy).toBeCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(1);
expect(refetchData2).toBe(invalidProxy);
} finally {
process.env.NODE_ENV = prevNODE_ENV;
Expand Down Expand Up @@ -1498,11 +1498,10 @@ test('prepass works', () => {
});

test('type casters', async () => {
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T
>() => T extends Y ? 1 : 2
? true
: false;
type Equals<X, Y> =
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
? true
: false;

const a1: {
a: string | undefined;
Expand Down Expand Up @@ -1541,7 +1540,7 @@ test('type casters', async () => {
h: [
{
i: number | undefined;
}
},
];
};
}
Expand Down
84 changes: 42 additions & 42 deletions packages/gqty/test/interfaces-unions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,23 @@ describe('interfaces and unions', () => {
`);

expect(queries).toMatchInlineSnapshot(`
[
{
"query": "query($a18aa4:NodeType!){a0b55f:node(type:$a18aa4){__typename ...on A{a}...on B{b}id}}",
"result": {
"data": {
"a0b55f": {
"__typename": "A",
"a": 1,
"id": "1",
},
},
},
"variables": {
"a18aa4": "A",
},
},
]
[
{
"query": "query($a18aa4:NodeType!){a0b55f:node(type:$a18aa4){__typename ...on A{a}...on B{b}id}}",
"result": {
"data": {
"a0b55f": {
"__typename": "A",
"a": 1,
"id": "1",
},
},
},
"variables": {
"a18aa4": "A",
},
},
]
`);

expect(nodeResult).toStrictEqual({
Expand Down Expand Up @@ -185,31 +185,31 @@ describe('interfaces and unions', () => {
}
`);
expect(queries).toMatchInlineSnapshot(`
[
{
"query": "query($a18aa4:NodeType!){a0b55f:node(type:$a18aa4){__typename ...on A{a node{__typename ...on A{id node{__typename ...on C{node{__typename ...on A{id}id}}id}}id}}...on B{b}id}}",
"result": {
"data": {
"a0b55f": {
"__typename": "A",
"a": 1,
"id": "1",
"node": {
"__typename": "A",
"id": "1",
"node": {
"__typename": "A",
"id": "1",
},
},
},
},
},
"variables": {
"a18aa4": "A",
},
},
]
[
{
"query": "query($a18aa4:NodeType!){a0b55f:node(type:$a18aa4){__typename ...on A{a node{__typename ...on A{id node{__typename ...on C{node{__typename ...on A{id}id}}id}}id}}...on B{b}id}}",
"result": {
"data": {
"a0b55f": {
"__typename": "A",
"a": 1,
"id": "1",
"node": {
"__typename": "A",
"id": "1",
"node": {
"__typename": "A",
"id": "1",
},
},
},
},
},
"variables": {
"a18aa4": "A",
},
},
]
`);
});
});

0 comments on commit dcd6ac2

Please sign in to comment.