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

Add test for modelsOnly option #441

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mst-gql",
"description": "Bindings for mobx-state-tree and GraphQL",
"license": "MIT",
"version": "0.17.2",
"version": "0.17.3",
"author": "Michel Weststrate",
"maintainers": [
{
Expand All @@ -25,6 +25,7 @@
"test:example-2": "cd examples/2-scaffolding && yarn start",
"test:example-6": "cd examples/6-scaffolding-ts-hasura && yarn start",
"test:mst-gql": "jest test",
"test:update-snapshot": "jest --updateSnapshot",
"watch": "jest test --watch",
"prepack": "pinst --disable",
"postpack": "pinst --enable"
Expand Down
269 changes: 269 additions & 0 deletions tests/generator/__snapshots__/generate.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2854,6 +2854,275 @@ export * from \\"./reactUtils\\"
]
`;

exports[`scaffolding with model generation only to work 1`] = `
Array [
Array [
"ModelBase",
"import { MSTGQLObject } from \\"mst-gql\\"

export const ModelBase = MSTGQLObject
",
false,
],
Array [
"MyUserModel.base",
"/* This is a mst-gql generated file, don't modify it manually */
/* eslint-disable */
/* tslint:disable */

import { IObservableArray } from \\"mobx\\"
import { types } from \\"mobx-state-tree\\"
import { MSTGQLRef, QueryBuilder, withTypedRefs } from \\"mst-gql\\"
import { ModelBase } from \\"./ModelBase\\"
import { PossiblyEmptyBoxModel, PossiblyEmptyBoxModelType } from \\"./PossiblyEmptyBoxModel\\"
import { RootStoreType } from \\"./index\\"


/* The TypeScript type that explicits the refs to other models in order to prevent a circular refs issue */
type Refs = {
emptyBoxes: IObservableArray<PossiblyEmptyBoxModelType>;
}

/**
* MyUserBase
* auto generated base class for the model MyUserModel.
*/
export const MyUserModelBase = withTypedRefs<Refs>()(ModelBase
.named('MyUser')
.props({
__typename: types.optional(types.literal(\\"my_user\\"), \\"my_user\\"),
id: types.identifier,
name: types.union(types.undefined, types.string),
avatar: types.union(types.undefined, types.string),
emptyBoxes: types.union(types.undefined, types.array(types.union(types.null, MSTGQLRef(types.late((): any => PossiblyEmptyBoxModel))))),
})
.views(self => ({
get store() {
return self.__getStore<RootStoreType>()
}
})))


",
true,
],
Array [
"MyUserModel",
"import { Instance } from \\"mobx-state-tree\\"
import { MyUserModelBase } from \\"./MyUserModel.base\\"

/* The TypeScript type of an instance of MyUserModel */
export interface MyUserModelType extends Instance<typeof MyUserModel.Type> {}



/**
* MyUserModel
*/
export const MyUserModel = MyUserModelBase
.actions(self => ({
// This is an auto-generated example action.
log() {
console.log(JSON.stringify(self))
}
}))
",
false,
],
Array [
"PossiblyEmptyBoxModel.base",
"/* This is a mst-gql generated file, don't modify it manually */
/* eslint-disable */
/* tslint:disable */

import { types } from \\"mobx-state-tree\\"
import { QueryBuilder } from \\"mst-gql\\"
import { ModelBase } from \\"./ModelBase\\"
import { RootStoreType } from \\"./index\\"


/**
* PossiblyEmptyBoxBase
* auto generated base class for the model PossiblyEmptyBoxModel.
*/
export const PossiblyEmptyBoxModelBase = ModelBase
.named('PossiblyEmptyBox')
.props({
__typename: types.optional(types.literal(\\"possibly_empty_box\\"), \\"possibly_empty_box\\"),
id: types.identifier,
label: types.union(types.undefined, types.string),
isEmpty: types.union(types.undefined, types.boolean),
})
.views(self => ({
get store() {
return self.__getStore<RootStoreType>()
}
}))


",
true,
],
Array [
"PossiblyEmptyBoxModel",
"import { Instance } from \\"mobx-state-tree\\"
import { PossiblyEmptyBoxModelBase } from \\"./PossiblyEmptyBoxModel.base\\"

/* The TypeScript type of an instance of PossiblyEmptyBoxModel */
export interface PossiblyEmptyBoxModelType extends Instance<typeof PossiblyEmptyBoxModel.Type> {}



/**
* PossiblyEmptyBoxModel
*/
export const PossiblyEmptyBoxModel = PossiblyEmptyBoxModelBase
.actions(self => ({
// This is an auto-generated example action.
log() {
console.log(JSON.stringify(self))
}
}))
",
false,
],
Array [
"QueryRootModel.base",
"/* This is a mst-gql generated file, don't modify it manually */
/* eslint-disable */
/* tslint:disable */

import { types } from \\"mobx-state-tree\\"
import { MSTGQLRef, QueryBuilder, withTypedRefs } from \\"mst-gql\\"
import { ModelBase } from \\"./ModelBase\\"
import { MyUserModel, MyUserModelType } from \\"./MyUserModel\\"
import { RootStoreType } from \\"./index\\"


/* The TypeScript type that explicits the refs to other models in order to prevent a circular refs issue */
type Refs = {
me: MyUserModelType;
}

/**
* QueryRootBase
* auto generated base class for the model QueryRootModel.
*/
export const QueryRootModelBase = withTypedRefs<Refs>()(ModelBase
.named('QueryRoot')
.props({
__typename: types.optional(types.literal(\\"query_root\\"), \\"query_root\\"),
me: types.union(types.undefined, types.null, MSTGQLRef(types.late((): any => MyUserModel))),
})
.views(self => ({
get store() {
return self.__getStore<RootStoreType>()
}
})))


",
true,
],
Array [
"QueryRootModel",
"import { Instance } from \\"mobx-state-tree\\"
import { QueryRootModelBase } from \\"./QueryRootModel.base\\"

/* The TypeScript type of an instance of QueryRootModel */
export interface QueryRootModelType extends Instance<typeof QueryRootModel.Type> {}



/**
* QueryRootModel
*/
export const QueryRootModel = QueryRootModelBase
.actions(self => ({
// This is an auto-generated example action.
log() {
console.log(JSON.stringify(self))
}
}))
",
false,
],
Array [
"RootStore",
"import { Instance } from \\"mobx-state-tree\\"
import { RootStoreBase } from \\"./RootStore.base\\"

export interface RootStoreType extends Instance<typeof RootStore.Type> {}

export const RootStore = RootStoreBase
.actions(self => ({
// This is an auto-generated example action.
log() {
console.log(JSON.stringify(self))
}
}))
",
false,
],
Array [
"RootStore.base",
"/* This is a mst-gql generated file, don't modify it manually */
/* eslint-disable */
/* tslint:disable */
import { ObservableMap } from \\"mobx\\"
import { types } from \\"mobx-state-tree\\"
import { MSTGQLStore, configureStoreMixin, QueryOptions, withTypedRefs } from \\"mst-gql\\"

import { MyUserModel, MyUserModelType } from \\"./MyUserModel\\"
import { PossiblyEmptyBoxModel, PossiblyEmptyBoxModelType } from \\"./PossiblyEmptyBoxModel\\"
import { QueryRootModel, QueryRootModelType } from \\"./QueryRootModel\\"



/* The TypeScript type that explicits the refs to other models in order to prevent a circular refs issue */
type Refs = {
myUsers: ObservableMap<string, MyUserModelType>,
possiblyEmptyBoxes: ObservableMap<string, PossiblyEmptyBoxModelType>
}


/**
* Enums for the names of base graphql actions
*/



/**
* Store, managing, among others, all the objects received through graphQL
*/
export const RootStoreBase = withTypedRefs<Refs>()(types.model()
.named(\\"RootStore\\")
.extend(configureStoreMixin([['my_user', () => MyUserModel], ['possibly_empty_box', () => PossiblyEmptyBoxModel], ['query_root', () => QueryRootModel]], ['my_user', 'possibly_empty_box'], \\"js\\"))
.props({
myUsers: types.optional(types.map(types.late((): any => MyUserModel)), {}),
possiblyEmptyBoxes: types.optional(types.map(types.late((): any => PossiblyEmptyBoxModel)), {})
})
.actions(self => ({
})))
",
true,
],
Array [
"index",
"/* This is a mst-gql generated file, don't modify it manually */
/* eslint-disable */
/* tslint:disable */

export * from \\"./MyUserModel\\"
export * from \\"./PossiblyEmptyBoxModel\\"
export * from \\"./QueryRootModel\\"
export * from \\"./RootStore\\"
",
true,
],
]
`;

exports[`scaffolding with scalar type 1`] = `
Array [
Array [
Expand Down
51 changes: 50 additions & 1 deletion tests/generator/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,56 @@ type query_root {
).toBeTruthy()
})

test("scaffolding with model generation only to work", () => {
const output = scaffold(
`

schema {
query: query_root
}

type my_user {
id: ID
name: String!
avatar: String!
emptyBoxes: [possibly_empty_box]!
}

type possibly_empty_box {
id: ID
label: String!
isEmpty: Boolean!
}

type query_root {
me: my_user
}
`,
{
roots: ["my_user", "possibly_empty_box"],
modelsOnly: true
}
)

expect(output).toMatchSnapshot()

// Should still have model props
expect(
hasFileContent(
findFile(output, "RootStore.base"),
"possiblyEmptyBoxes: types.optional(types.map(types.late((): any => PossiblyEmptyBoxModel)), {})"
)
).toBeTruthy()

// Should not have queryMe() action this time
expect(
!hasFileContent(
findFile(output, "RootStore.base"),
"queryMe(variables?: { }, resultSelector: string | ((qb: MyUserModelSelector) => MyUserModelSelector) = myUserModelPrimitives.toString(), options: QueryOptions = {}) {"
)
).toBeTruthy()
})

test("interface field type to work", () => {
const output = scaffold(
`
Expand Down Expand Up @@ -176,7 +226,6 @@ type Query {
namingConvention: "asis"
}
)
console.log("+++ output", output)
expect(output).toMatchSnapshot()

expect(findFile(output, "SearchItemModelSelector")).toBeTruthy()
Expand Down
Loading