Skip to content

Commit 94dd22f

Browse files
committed
Add test for modelsOnly option
1 parent 0b7b51f commit 94dd22f

File tree

3 files changed

+321
-2
lines changed

3 files changed

+321
-2
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "mst-gql",
33
"description": "Bindings for mobx-state-tree and GraphQL",
44
"license": "MIT",
5-
"version": "0.17.2",
5+
"version": "0.17.3",
66
"author": "Michel Weststrate",
77
"maintainers": [
88
{
@@ -25,6 +25,7 @@
2525
"test:example-2": "cd examples/2-scaffolding && yarn start",
2626
"test:example-6": "cd examples/6-scaffolding-ts-hasura && yarn start",
2727
"test:mst-gql": "jest test",
28+
"test:update-snapshot": "jest --updateSnapshot",
2829
"watch": "jest test --watch",
2930
"prepack": "pinst --disable",
3031
"postpack": "pinst --enable"

tests/generator/__snapshots__/generate.test.js.snap

+269
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,275 @@ export * from \\"./reactUtils\\"
28542854
]
28552855
`;
28562856
2857+
exports[`scaffolding with model generation only to work 1`] = `
2858+
Array [
2859+
Array [
2860+
"ModelBase",
2861+
"import { MSTGQLObject } from \\"mst-gql\\"
2862+
2863+
export const ModelBase = MSTGQLObject
2864+
",
2865+
false,
2866+
],
2867+
Array [
2868+
"MyUserModel.base",
2869+
"/* This is a mst-gql generated file, don't modify it manually */
2870+
/* eslint-disable */
2871+
/* tslint:disable */
2872+
2873+
import { IObservableArray } from \\"mobx\\"
2874+
import { types } from \\"mobx-state-tree\\"
2875+
import { MSTGQLRef, QueryBuilder, withTypedRefs } from \\"mst-gql\\"
2876+
import { ModelBase } from \\"./ModelBase\\"
2877+
import { PossiblyEmptyBoxModel, PossiblyEmptyBoxModelType } from \\"./PossiblyEmptyBoxModel\\"
2878+
import { RootStoreType } from \\"./index\\"
2879+
2880+
2881+
/* The TypeScript type that explicits the refs to other models in order to prevent a circular refs issue */
2882+
type Refs = {
2883+
emptyBoxes: IObservableArray<PossiblyEmptyBoxModelType>;
2884+
}
2885+
2886+
/**
2887+
* MyUserBase
2888+
* auto generated base class for the model MyUserModel.
2889+
*/
2890+
export const MyUserModelBase = withTypedRefs<Refs>()(ModelBase
2891+
.named('MyUser')
2892+
.props({
2893+
__typename: types.optional(types.literal(\\"my_user\\"), \\"my_user\\"),
2894+
id: types.identifier,
2895+
name: types.union(types.undefined, types.string),
2896+
avatar: types.union(types.undefined, types.string),
2897+
emptyBoxes: types.union(types.undefined, types.array(types.union(types.null, MSTGQLRef(types.late((): any => PossiblyEmptyBoxModel))))),
2898+
})
2899+
.views(self => ({
2900+
get store() {
2901+
return self.__getStore<RootStoreType>()
2902+
}
2903+
})))
2904+
2905+
2906+
",
2907+
true,
2908+
],
2909+
Array [
2910+
"MyUserModel",
2911+
"import { Instance } from \\"mobx-state-tree\\"
2912+
import { MyUserModelBase } from \\"./MyUserModel.base\\"
2913+
2914+
/* The TypeScript type of an instance of MyUserModel */
2915+
export interface MyUserModelType extends Instance<typeof MyUserModel.Type> {}
2916+
2917+
2918+
2919+
/**
2920+
* MyUserModel
2921+
*/
2922+
export const MyUserModel = MyUserModelBase
2923+
.actions(self => ({
2924+
// This is an auto-generated example action.
2925+
log() {
2926+
console.log(JSON.stringify(self))
2927+
}
2928+
}))
2929+
",
2930+
false,
2931+
],
2932+
Array [
2933+
"PossiblyEmptyBoxModel.base",
2934+
"/* This is a mst-gql generated file, don't modify it manually */
2935+
/* eslint-disable */
2936+
/* tslint:disable */
2937+
2938+
import { types } from \\"mobx-state-tree\\"
2939+
import { QueryBuilder } from \\"mst-gql\\"
2940+
import { ModelBase } from \\"./ModelBase\\"
2941+
import { RootStoreType } from \\"./index\\"
2942+
2943+
2944+
/**
2945+
* PossiblyEmptyBoxBase
2946+
* auto generated base class for the model PossiblyEmptyBoxModel.
2947+
*/
2948+
export const PossiblyEmptyBoxModelBase = ModelBase
2949+
.named('PossiblyEmptyBox')
2950+
.props({
2951+
__typename: types.optional(types.literal(\\"possibly_empty_box\\"), \\"possibly_empty_box\\"),
2952+
id: types.identifier,
2953+
label: types.union(types.undefined, types.string),
2954+
isEmpty: types.union(types.undefined, types.boolean),
2955+
})
2956+
.views(self => ({
2957+
get store() {
2958+
return self.__getStore<RootStoreType>()
2959+
}
2960+
}))
2961+
2962+
2963+
",
2964+
true,
2965+
],
2966+
Array [
2967+
"PossiblyEmptyBoxModel",
2968+
"import { Instance } from \\"mobx-state-tree\\"
2969+
import { PossiblyEmptyBoxModelBase } from \\"./PossiblyEmptyBoxModel.base\\"
2970+
2971+
/* The TypeScript type of an instance of PossiblyEmptyBoxModel */
2972+
export interface PossiblyEmptyBoxModelType extends Instance<typeof PossiblyEmptyBoxModel.Type> {}
2973+
2974+
2975+
2976+
/**
2977+
* PossiblyEmptyBoxModel
2978+
*/
2979+
export const PossiblyEmptyBoxModel = PossiblyEmptyBoxModelBase
2980+
.actions(self => ({
2981+
// This is an auto-generated example action.
2982+
log() {
2983+
console.log(JSON.stringify(self))
2984+
}
2985+
}))
2986+
",
2987+
false,
2988+
],
2989+
Array [
2990+
"QueryRootModel.base",
2991+
"/* This is a mst-gql generated file, don't modify it manually */
2992+
/* eslint-disable */
2993+
/* tslint:disable */
2994+
2995+
import { types } from \\"mobx-state-tree\\"
2996+
import { MSTGQLRef, QueryBuilder, withTypedRefs } from \\"mst-gql\\"
2997+
import { ModelBase } from \\"./ModelBase\\"
2998+
import { MyUserModel, MyUserModelType } from \\"./MyUserModel\\"
2999+
import { RootStoreType } from \\"./index\\"
3000+
3001+
3002+
/* The TypeScript type that explicits the refs to other models in order to prevent a circular refs issue */
3003+
type Refs = {
3004+
me: MyUserModelType;
3005+
}
3006+
3007+
/**
3008+
* QueryRootBase
3009+
* auto generated base class for the model QueryRootModel.
3010+
*/
3011+
export const QueryRootModelBase = withTypedRefs<Refs>()(ModelBase
3012+
.named('QueryRoot')
3013+
.props({
3014+
__typename: types.optional(types.literal(\\"query_root\\"), \\"query_root\\"),
3015+
me: types.union(types.undefined, types.null, MSTGQLRef(types.late((): any => MyUserModel))),
3016+
})
3017+
.views(self => ({
3018+
get store() {
3019+
return self.__getStore<RootStoreType>()
3020+
}
3021+
})))
3022+
3023+
3024+
",
3025+
true,
3026+
],
3027+
Array [
3028+
"QueryRootModel",
3029+
"import { Instance } from \\"mobx-state-tree\\"
3030+
import { QueryRootModelBase } from \\"./QueryRootModel.base\\"
3031+
3032+
/* The TypeScript type of an instance of QueryRootModel */
3033+
export interface QueryRootModelType extends Instance<typeof QueryRootModel.Type> {}
3034+
3035+
3036+
3037+
/**
3038+
* QueryRootModel
3039+
*/
3040+
export const QueryRootModel = QueryRootModelBase
3041+
.actions(self => ({
3042+
// This is an auto-generated example action.
3043+
log() {
3044+
console.log(JSON.stringify(self))
3045+
}
3046+
}))
3047+
",
3048+
false,
3049+
],
3050+
Array [
3051+
"RootStore",
3052+
"import { Instance } from \\"mobx-state-tree\\"
3053+
import { RootStoreBase } from \\"./RootStore.base\\"
3054+
3055+
export interface RootStoreType extends Instance<typeof RootStore.Type> {}
3056+
3057+
export const RootStore = RootStoreBase
3058+
.actions(self => ({
3059+
// This is an auto-generated example action.
3060+
log() {
3061+
console.log(JSON.stringify(self))
3062+
}
3063+
}))
3064+
",
3065+
false,
3066+
],
3067+
Array [
3068+
"RootStore.base",
3069+
"/* This is a mst-gql generated file, don't modify it manually */
3070+
/* eslint-disable */
3071+
/* tslint:disable */
3072+
import { ObservableMap } from \\"mobx\\"
3073+
import { types } from \\"mobx-state-tree\\"
3074+
import { MSTGQLStore, configureStoreMixin, QueryOptions, withTypedRefs } from \\"mst-gql\\"
3075+
3076+
import { MyUserModel, MyUserModelType } from \\"./MyUserModel\\"
3077+
import { PossiblyEmptyBoxModel, PossiblyEmptyBoxModelType } from \\"./PossiblyEmptyBoxModel\\"
3078+
import { QueryRootModel, QueryRootModelType } from \\"./QueryRootModel\\"
3079+
3080+
3081+
3082+
/* The TypeScript type that explicits the refs to other models in order to prevent a circular refs issue */
3083+
type Refs = {
3084+
myUsers: ObservableMap<string, MyUserModelType>,
3085+
possiblyEmptyBoxes: ObservableMap<string, PossiblyEmptyBoxModelType>
3086+
}
3087+
3088+
3089+
/**
3090+
* Enums for the names of base graphql actions
3091+
*/
3092+
3093+
3094+
3095+
/**
3096+
* Store, managing, among others, all the objects received through graphQL
3097+
*/
3098+
export const RootStoreBase = withTypedRefs<Refs>()(types.model()
3099+
.named(\\"RootStore\\")
3100+
.extend(configureStoreMixin([['my_user', () => MyUserModel], ['possibly_empty_box', () => PossiblyEmptyBoxModel], ['query_root', () => QueryRootModel]], ['my_user', 'possibly_empty_box'], \\"js\\"))
3101+
.props({
3102+
myUsers: types.optional(types.map(types.late((): any => MyUserModel)), {}),
3103+
possiblyEmptyBoxes: types.optional(types.map(types.late((): any => PossiblyEmptyBoxModel)), {})
3104+
})
3105+
.actions(self => ({
3106+
})))
3107+
",
3108+
true,
3109+
],
3110+
Array [
3111+
"index",
3112+
"/* This is a mst-gql generated file, don't modify it manually */
3113+
/* eslint-disable */
3114+
/* tslint:disable */
3115+
3116+
export * from \\"./MyUserModel\\"
3117+
export * from \\"./PossiblyEmptyBoxModel\\"
3118+
export * from \\"./QueryRootModel\\"
3119+
export * from \\"./RootStore\\"
3120+
",
3121+
true,
3122+
],
3123+
]
3124+
`;
3125+
28573126
exports[`scaffolding with scalar type 1`] = `
28583127
Array [
28593128
Array [

tests/generator/generate.test.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,56 @@ type query_root {
107107
).toBeTruthy()
108108
})
109109

110+
test("scaffolding with model generation only to work", () => {
111+
const output = scaffold(
112+
`
113+
114+
schema {
115+
query: query_root
116+
}
117+
118+
type my_user {
119+
id: ID
120+
name: String!
121+
avatar: String!
122+
emptyBoxes: [possibly_empty_box]!
123+
}
124+
125+
type possibly_empty_box {
126+
id: ID
127+
label: String!
128+
isEmpty: Boolean!
129+
}
130+
131+
type query_root {
132+
me: my_user
133+
}
134+
`,
135+
{
136+
roots: ["my_user", "possibly_empty_box"],
137+
modelsOnly: true
138+
}
139+
)
140+
141+
expect(output).toMatchSnapshot()
142+
143+
// Should still have model props
144+
expect(
145+
hasFileContent(
146+
findFile(output, "RootStore.base"),
147+
"possiblyEmptyBoxes: types.optional(types.map(types.late((): any => PossiblyEmptyBoxModel)), {})"
148+
)
149+
).toBeTruthy()
150+
151+
// Should not have queryMe() action this time
152+
expect(
153+
!hasFileContent(
154+
findFile(output, "RootStore.base"),
155+
"queryMe(variables?: { }, resultSelector: string | ((qb: MyUserModelSelector) => MyUserModelSelector) = myUserModelPrimitives.toString(), options: QueryOptions = {}) {"
156+
)
157+
).toBeTruthy()
158+
})
159+
110160
test("interface field type to work", () => {
111161
const output = scaffold(
112162
`
@@ -176,7 +226,6 @@ type Query {
176226
namingConvention: "asis"
177227
}
178228
)
179-
console.log("+++ output", output)
180229
expect(output).toMatchSnapshot()
181230

182231
expect(findFile(output, "SearchItemModelSelector")).toBeTruthy()

0 commit comments

Comments
 (0)