Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: global Token of a module should be accessible by itself (singleton) #2278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/graphql-modules/tests/di-providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,67 @@ test('Global Token (module) should use other local tokens (singleton)', async ()
expect(logger).toHaveBeenNthCalledWith(2, 'info');
});

test('Global Token of a module should be accessible by itself (singleton)', async () => {
@Injectable({
scope: Scope.Singleton,
global: true,
})
class Data {
lorem() {
return 'ipsum';
}
}

@Injectable({
scope: Scope.Singleton,
})
class AppData {
constructor(private data: Data) {}

ipsum() {
return this.data.lorem();
}
}

const fooModule = createModule({
id: 'foo',
providers: [Data, AppData],
typeDefs: gql`
type Query {
foo: String!
}
`,
resolvers: {
Query: {
foo(_parent: {}, _args: {}, { injector }: GraphQLModulesModuleContext) {
return injector.get(AppData).ipsum();
},
},
},
});

const app = createApplication({
modules: [fooModule],
});

const contextValue = { request: {}, response: {} };
const document = gql`
{
foo
}
`;

const result = await testkit.execute(app, {
contextValue,
document,
});

expect(result.errors).toBeUndefined();
expect(result.data).toEqual({
foo: 'ipsum',
});
});

test('instantiate all singleton providers', async () => {
const spies = {
logger: jest.fn(),
Expand Down