custom-fields.e2e-spec.ts 15 KB

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