custom-fields.e2e-spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // Force the timezone to avoid tests failing in other locales
  2. process.env.TZ = 'UTC';
  3. import { LanguageCode } from '@vendure/common/lib/generated-types';
  4. import { CustomFields } from '@vendure/core/dist/config/custom-field/custom-field-types';
  5. import { createTestEnvironment } from '@vendure/testing';
  6. import gql from 'graphql-tag';
  7. import path from 'path';
  8. import { initialData } from '../../../e2e-common/e2e-initial-data';
  9. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  10. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  11. // tslint:disable:no-non-null-assertion
  12. const customConfig = {
  13. ...testConfig,
  14. ...{
  15. customFields: {
  16. Product: [
  17. { name: 'nullable', type: 'string' },
  18. { name: 'notNullable', type: 'string', nullable: false, defaultValue: '' },
  19. { name: 'stringWithDefault', type: 'string', defaultValue: 'hello' },
  20. { name: 'localeStringWithDefault', type: 'localeString', defaultValue: 'hola' },
  21. { name: 'intWithDefault', type: 'int', defaultValue: 5 },
  22. { name: 'floatWithDefault', type: 'float', defaultValue: 5.5 },
  23. { name: 'booleanWithDefault', type: 'boolean', defaultValue: true },
  24. {
  25. name: 'dateTimeWithDefault',
  26. type: 'datetime',
  27. defaultValue: new Date('2019-04-30T12:59:16.4158386Z'),
  28. },
  29. { name: 'validateString', type: 'string', pattern: '^[0-9][a-z]+$' },
  30. { name: 'validateLocaleString', type: 'localeString', pattern: '^[0-9][a-z]+$' },
  31. { name: 'validateInt', type: 'int', min: 0, max: 10 },
  32. { name: 'validateFloat', type: 'float', min: 0.5, max: 10.5 },
  33. {
  34. name: 'validateDateTime',
  35. type: 'datetime',
  36. min: '2019-01-01T08:30',
  37. max: '2019-06-01T08:30',
  38. },
  39. {
  40. name: 'validateFn1',
  41. type: 'string',
  42. validate: value => {
  43. if (value !== 'valid') {
  44. return `The value ['${value}'] is not valid`;
  45. }
  46. },
  47. },
  48. {
  49. name: 'validateFn2',
  50. type: 'string',
  51. validate: value => {
  52. if (value !== 'valid') {
  53. return [
  54. {
  55. languageCode: LanguageCode.en,
  56. value: `The value ['${value}'] is not valid`,
  57. },
  58. ];
  59. }
  60. },
  61. },
  62. {
  63. name: 'stringWithOptions',
  64. type: 'string',
  65. options: [{ value: 'small' }, { value: 'medium' }, { value: 'large' }],
  66. },
  67. {
  68. name: 'nonPublic',
  69. type: 'string',
  70. defaultValue: 'hi!',
  71. public: false,
  72. },
  73. {
  74. name: 'public',
  75. type: 'string',
  76. defaultValue: 'ho!',
  77. public: true,
  78. },
  79. {
  80. name: 'longString',
  81. type: 'string',
  82. },
  83. ],
  84. Facet: [
  85. {
  86. name: 'translated',
  87. type: 'localeString',
  88. },
  89. ],
  90. } as CustomFields,
  91. },
  92. };
  93. describe('Custom fields', () => {
  94. const { server, adminClient, shopClient } = createTestEnvironment(customConfig);
  95. beforeAll(async () => {
  96. await server.init({
  97. dataDir: path.join(__dirname, '__data__'),
  98. initialData,
  99. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  100. customerCount: 1,
  101. });
  102. await adminClient.asSuperAdmin();
  103. }, TEST_SETUP_TIMEOUT_MS);
  104. afterAll(async () => {
  105. await server.destroy();
  106. });
  107. it('globalSettings.serverConfig.customFieldConfig', async () => {
  108. const { globalSettings } = await adminClient.query(gql`
  109. query {
  110. globalSettings {
  111. serverConfig {
  112. customFieldConfig {
  113. Product {
  114. ... on CustomField {
  115. name
  116. type
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. `);
  124. expect(globalSettings.serverConfig.customFieldConfig).toEqual({
  125. Product: [
  126. { name: 'nullable', type: 'string' },
  127. { name: 'notNullable', type: 'string' },
  128. { name: 'stringWithDefault', type: 'string' },
  129. { name: 'localeStringWithDefault', type: 'localeString' },
  130. { name: 'intWithDefault', type: 'int' },
  131. { name: 'floatWithDefault', type: 'float' },
  132. { name: 'booleanWithDefault', type: 'boolean' },
  133. { name: 'dateTimeWithDefault', type: 'datetime' },
  134. { name: 'validateString', type: 'string' },
  135. { name: 'validateLocaleString', type: 'localeString' },
  136. { name: 'validateInt', type: 'int' },
  137. { name: 'validateFloat', type: 'float' },
  138. { name: 'validateDateTime', type: 'datetime' },
  139. { name: 'validateFn1', type: 'string' },
  140. { name: 'validateFn2', type: 'string' },
  141. { name: 'stringWithOptions', type: 'string' },
  142. { name: 'nonPublic', type: 'string' },
  143. { name: 'public', type: 'string' },
  144. { name: 'longString', type: 'string' },
  145. ],
  146. });
  147. });
  148. it('get nullable with no default', async () => {
  149. const { product } = await adminClient.query(gql`
  150. query {
  151. product(id: "T_1") {
  152. id
  153. name
  154. customFields {
  155. nullable
  156. }
  157. }
  158. }
  159. `);
  160. expect(product).toEqual({
  161. id: 'T_1',
  162. name: 'Laptop',
  163. customFields: {
  164. nullable: null,
  165. },
  166. });
  167. });
  168. it('get entity with localeString only', async () => {
  169. const { facet } = await adminClient.query(gql`
  170. query {
  171. facet(id: "T_1") {
  172. id
  173. name
  174. customFields {
  175. translated
  176. }
  177. }
  178. }
  179. `);
  180. expect(facet).toEqual({
  181. id: 'T_1',
  182. name: 'category',
  183. customFields: {
  184. translated: null,
  185. },
  186. });
  187. });
  188. it('get fields with default values', async () => {
  189. const { product } = await adminClient.query(gql`
  190. query {
  191. product(id: "T_1") {
  192. id
  193. name
  194. customFields {
  195. stringWithDefault
  196. localeStringWithDefault
  197. intWithDefault
  198. floatWithDefault
  199. booleanWithDefault
  200. dateTimeWithDefault
  201. }
  202. }
  203. }
  204. `);
  205. expect(product).toEqual({
  206. id: 'T_1',
  207. name: 'Laptop',
  208. customFields: {
  209. stringWithDefault: 'hello',
  210. localeStringWithDefault: 'hola',
  211. intWithDefault: 5,
  212. floatWithDefault: 5.5,
  213. booleanWithDefault: true,
  214. dateTimeWithDefault: '2019-04-30T12:59:16.415Z',
  215. },
  216. });
  217. });
  218. it(
  219. 'update non-nullable field',
  220. assertThrowsWithMessage(async () => {
  221. await adminClient.query(gql`
  222. mutation {
  223. updateProduct(input: { id: "T_1", customFields: { notNullable: null } }) {
  224. id
  225. }
  226. }
  227. `);
  228. }, 'NOT NULL constraint failed: product.customFieldsNotnullable'),
  229. );
  230. it('string length allows long strings', async () => {
  231. const longString = Array.from({ length: 5000 }, v => 'hello there!').join(' ');
  232. const result = await adminClient.query(
  233. gql`
  234. mutation($stringValue: String!) {
  235. updateProduct(input: { id: "T_1", customFields: { longString: $stringValue } }) {
  236. id
  237. customFields {
  238. longString
  239. }
  240. }
  241. }
  242. `,
  243. { stringValue: longString },
  244. );
  245. expect(result.updateProduct.customFields.longString).toBe(longString);
  246. });
  247. describe('validation', () => {
  248. it(
  249. 'invalid string',
  250. assertThrowsWithMessage(async () => {
  251. await adminClient.query(gql`
  252. mutation {
  253. updateProduct(input: { id: "T_1", customFields: { validateString: "hello" } }) {
  254. id
  255. }
  256. }
  257. `);
  258. }, `The custom field 'validateString' value ['hello'] does not match the pattern [^[0-9][a-z]+$]`),
  259. );
  260. it(
  261. 'invalid string option',
  262. assertThrowsWithMessage(async () => {
  263. await adminClient.query(gql`
  264. mutation {
  265. updateProduct(input: { id: "T_1", customFields: { stringWithOptions: "tiny" } }) {
  266. id
  267. }
  268. }
  269. `);
  270. }, `The custom field 'stringWithOptions' value ['tiny'] is invalid. Valid options are ['small', 'medium', 'large']`),
  271. );
  272. it('valid string option', async () => {
  273. const { updateProduct } = await adminClient.query(gql`
  274. mutation {
  275. updateProduct(input: { id: "T_1", customFields: { stringWithOptions: "medium" } }) {
  276. id
  277. customFields {
  278. stringWithOptions
  279. }
  280. }
  281. }
  282. `);
  283. expect(updateProduct.customFields.stringWithOptions).toBe('medium');
  284. });
  285. it(
  286. 'invalid localeString',
  287. assertThrowsWithMessage(async () => {
  288. await adminClient.query(gql`
  289. mutation {
  290. updateProduct(
  291. input: {
  292. id: "T_1"
  293. translations: [
  294. {
  295. id: "T_1"
  296. languageCode: en
  297. customFields: { validateLocaleString: "servus" }
  298. }
  299. ]
  300. }
  301. ) {
  302. id
  303. }
  304. }
  305. `);
  306. }, `The custom field 'validateLocaleString' value ['servus'] does not match the pattern [^[0-9][a-z]+$]`),
  307. );
  308. it(
  309. 'invalid int',
  310. assertThrowsWithMessage(async () => {
  311. await adminClient.query(gql`
  312. mutation {
  313. updateProduct(input: { id: "T_1", customFields: { validateInt: 12 } }) {
  314. id
  315. }
  316. }
  317. `);
  318. }, `The custom field 'validateInt' value [12] is greater than the maximum [10]`),
  319. );
  320. it(
  321. 'invalid float',
  322. assertThrowsWithMessage(async () => {
  323. await adminClient.query(gql`
  324. mutation {
  325. updateProduct(input: { id: "T_1", customFields: { validateFloat: 10.6 } }) {
  326. id
  327. }
  328. }
  329. `);
  330. }, `The custom field 'validateFloat' value [10.6] is greater than the maximum [10.5]`),
  331. );
  332. it(
  333. 'invalid datetime',
  334. assertThrowsWithMessage(async () => {
  335. await adminClient.query(gql`
  336. mutation {
  337. updateProduct(
  338. input: {
  339. id: "T_1"
  340. customFields: { validateDateTime: "2019-01-01T05:25:00.000Z" }
  341. }
  342. ) {
  343. id
  344. }
  345. }
  346. `);
  347. }, `The custom field 'validateDateTime' value [2019-01-01T05:25:00.000Z] is less than the minimum [2019-01-01T08:30]`),
  348. );
  349. it(
  350. 'invalid validate function with string',
  351. assertThrowsWithMessage(async () => {
  352. await adminClient.query(gql`
  353. mutation {
  354. updateProduct(input: { id: "T_1", customFields: { validateFn1: "invalid" } }) {
  355. id
  356. }
  357. }
  358. `);
  359. }, `The value ['invalid'] is not valid`),
  360. );
  361. it(
  362. 'invalid validate function with localized string',
  363. assertThrowsWithMessage(async () => {
  364. await adminClient.query(gql`
  365. mutation {
  366. updateProduct(input: { id: "T_1", customFields: { validateFn2: "invalid" } }) {
  367. id
  368. }
  369. }
  370. `);
  371. }, `The value ['invalid'] is not valid`),
  372. );
  373. });
  374. describe('public access', () => {
  375. it(
  376. 'non-public throws for Shop API',
  377. assertThrowsWithMessage(async () => {
  378. await shopClient.query(gql`
  379. query {
  380. product(id: "T_1") {
  381. id
  382. customFields {
  383. nonPublic
  384. }
  385. }
  386. }
  387. `);
  388. }, `Cannot query field "nonPublic" on type "ProductCustomFields"`),
  389. );
  390. it('publicly accessible via Shop API', async () => {
  391. const { product } = await shopClient.query(gql`
  392. query {
  393. product(id: "T_1") {
  394. id
  395. customFields {
  396. public
  397. }
  398. }
  399. }
  400. `);
  401. expect(product.customFields.public).toBe('ho!');
  402. });
  403. });
  404. describe('sort & filter', () => {
  405. it('can sort by custom fields', async () => {
  406. const { products } = await adminClient.query(gql`
  407. query {
  408. products(options: { sort: { nullable: ASC } }) {
  409. totalItems
  410. }
  411. }
  412. `);
  413. expect(products.totalItems).toBe(1);
  414. });
  415. it('can filter by custom fields', async () => {
  416. const { products } = await adminClient.query(gql`
  417. query {
  418. products(options: { filter: { stringWithDefault: { contains: "hello" } } }) {
  419. totalItems
  420. }
  421. }
  422. `);
  423. expect(products.totalItems).toBe(1);
  424. });
  425. });
  426. });