-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for useProfile and usePublication
- Loading branch information
Showing
8 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/api-bindings/src/apollo/cache/createProfileFieldPolicy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
defaultDataIdFromObject, | ||
FieldFunctionOptions, | ||
FieldPolicy, | ||
Reference, | ||
StoreObject, | ||
} from '@apollo/client'; | ||
import { never } from '@lens-protocol/shared-kernel'; | ||
|
||
import { Profile, ProfileRequest } from '../../lens'; | ||
|
||
function isProfile(value: StoreObject | undefined): value is Profile { | ||
return value?.__typename === 'Profile'; | ||
} | ||
|
||
const identifierPattern = | ||
defaultDataIdFromObject({ __typename: 'Profile', id: '0x[a-fA-F0-9]{2,}' }) ?? never(); | ||
const identifierMatcher = new RegExp(`^${identifierPattern}$`); | ||
|
||
export function createProfileFieldPolicy(): FieldPolicy< | ||
Profile, | ||
Profile, | ||
Reference, | ||
FieldFunctionOptions<{ request: ProfileRequest }> | ||
> { | ||
return { | ||
keyArgs: [['request', ['forProfileId', 'forHandle']]], | ||
|
||
read(_, { args, toReference, cache }) { | ||
if (args?.request.forProfileId) { | ||
return toReference({ | ||
__typename: 'Profile', | ||
id: args.request.forProfileId, | ||
}); | ||
} | ||
|
||
const normalizedCacheObject = cache.extract(true); | ||
|
||
for (const key in normalizedCacheObject) { | ||
const value = normalizedCacheObject[key]; | ||
|
||
if ( | ||
identifierMatcher.test(key) && | ||
isProfile(value) && | ||
value.handle === args?.request.forHandle | ||
) { | ||
return toReference(value); | ||
} | ||
} | ||
return; | ||
}, | ||
}; | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/api-bindings/src/apollo/cache/createProfileTypePolicy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { StrictTypedTypePolicies } from '../../lens'; | ||
|
||
export function createProfileTypePolicy(): StrictTypedTypePolicies['Profile'] { | ||
return { | ||
fields: {}, | ||
}; | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/api-bindings/src/apollo/cache/createPublicationFieldPolicy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { FieldFunctionOptions, FieldPolicy, Reference } from '@apollo/client'; | ||
|
||
import { AnyPublication, PublicationRequest } from '../../lens'; | ||
|
||
export function createPublicationFieldPolicy(): FieldPolicy< | ||
AnyPublication, | ||
AnyPublication, | ||
Reference, | ||
FieldFunctionOptions<{ request: PublicationRequest }> | ||
> { | ||
return { | ||
keyArgs: [['request', ['forId', 'forHandle']]], | ||
|
||
read(_, { args, toReference }) { | ||
if (args?.request.forId) { | ||
return toReference({ | ||
__typename: 'Publication', | ||
id: args.request.forId, | ||
}); | ||
} | ||
|
||
return; | ||
}, | ||
}; | ||
} |
4 changes: 1 addition & 3 deletions
4
packages/api-bindings/src/apollo/cache/createPublicationTypePolicy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import { StoreObject } from '@apollo/client'; | ||
|
||
const publicationTypename = 'Publication'; | ||
|
||
export function createPublicationTypePolicy() { | ||
return { | ||
keyFields: ({ id }: Readonly<StoreObject>) => `${publicationTypename}:${String(id)}`, | ||
keyFields: ({ id }: Readonly<StoreObject>) => `Publication:${String(id)}`, | ||
} as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { mockProfileFragment, mockProfileResponse } from '@lens-protocol/api-bindings/mocks'; | ||
import { waitFor } from '@testing-library/react'; | ||
|
||
import { NotFoundError } from '../../NotFoundError'; | ||
import { setupHookTestScenario } from '../../__helpers__/setupHookTestScenario'; | ||
import { useProfile } from '../useProfile'; | ||
|
||
describe(`Given the ${useProfile.name} hook`, () => { | ||
const profile = mockProfileFragment(); | ||
const expectations = { __typename: 'Profile', id: profile.id }; | ||
|
||
describe.each([ | ||
{ | ||
description: 'when invoked with a profile id', | ||
args: { forProfileId: profile.id }, | ||
}, | ||
{ | ||
description: 'when invoked with a profile handle', | ||
args: { forHandle: profile.handle }, | ||
}, | ||
])('$description', ({ args }) => { | ||
it('should settle with the profile data', async () => { | ||
const { renderHook } = setupHookTestScenario([ | ||
mockProfileResponse({ | ||
variables: { | ||
request: args, | ||
}, | ||
result: profile, | ||
}), | ||
]); | ||
|
||
const { result } = renderHook(() => useProfile(args)); | ||
|
||
await waitFor(() => expect(result.current.loading).toBeFalsy()); | ||
expect(result.current.data).toMatchObject(expectations); | ||
}); | ||
|
||
it(`should settle with a ${NotFoundError.name} if not found`, async () => { | ||
const { renderHook } = setupHookTestScenario([ | ||
mockProfileResponse({ | ||
variables: { | ||
request: args, | ||
}, | ||
result: null, | ||
}), | ||
]); | ||
|
||
const { result } = renderHook(() => useProfile(args)); | ||
|
||
await waitFor(() => expect(result.current.loading).toBeFalsy()); | ||
expect(result.current.error).toBeInstanceOf(NotFoundError); | ||
}); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/react/src/publication/__tests__/usePublication.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { mockPostFragment, mockPublicationResponse } from '@lens-protocol/api-bindings/mocks'; | ||
import { waitFor } from '@testing-library/react'; | ||
|
||
import { NotFoundError } from '../../NotFoundError'; | ||
import { setupHookTestScenario } from '../../__helpers__/setupHookTestScenario'; | ||
import { usePublication } from '../usePublication'; | ||
|
||
describe(`Given the ${usePublication.name} hook`, () => { | ||
const publication = mockPostFragment(); | ||
const expectations = { __typename: 'Post', id: publication.id }; | ||
|
||
describe('when the queried publication exists', () => { | ||
const { renderHook } = setupHookTestScenario([ | ||
mockPublicationResponse({ | ||
variables: { | ||
request: { | ||
forId: publication.id, | ||
}, | ||
}, | ||
publication, | ||
}), | ||
]); | ||
|
||
it('should settle with the publication data', async () => { | ||
const { result } = renderHook(() => usePublication({ forId: publication.id })); | ||
|
||
await waitFor(() => expect(result.current.loading).toBeFalsy()); | ||
expect(result.current.data).toMatchObject(expectations); | ||
}); | ||
}); | ||
|
||
describe('when the queried publication does not exist', () => { | ||
const { renderHook } = setupHookTestScenario([ | ||
mockPublicationResponse({ | ||
variables: { | ||
request: { | ||
forId: publication.id, | ||
}, | ||
}, | ||
publication: null, | ||
}), | ||
]); | ||
|
||
it(`should settle with a ${NotFoundError.name} state`, async () => { | ||
const { result } = renderHook(() => usePublication({ forId: publication.id })); | ||
|
||
await waitFor(() => expect(result.current.loading).toBeFalsy()); | ||
expect(result.current.error).toBeInstanceOf(NotFoundError); | ||
}); | ||
}); | ||
}); |