@elasticpath/component-test-framework 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/api.js CHANGED
@@ -17,22 +17,22 @@ const Logger = require("./logger");
17
17
  const utils_1 = require("./utils");
18
18
  class Api {
19
19
  constructor() {
20
- const { CUCUMBER_BASE_URL, CUCUMBER_LOGGING_LEVEL } = process.env;
20
+ const { CTF_BASE_URL, CTF_LOGGING_LEVEL } = process.env;
21
21
  this.defaultHeaders = {
22
22
  'content-type': 'application/json',
23
23
  };
24
24
  this.headers = this.defaultHeaders;
25
25
  this.axiosInstance = axios_1.default.create({
26
- baseURL: CUCUMBER_BASE_URL,
26
+ baseURL: CTF_BASE_URL,
27
27
  timeout: 4000,
28
- timeoutErrorMessage: `[Fail] Api request timed out. Is the service running on ${CUCUMBER_BASE_URL}`,
28
+ timeoutErrorMessage: `[Fail] Api request timed out. Is the service running on ${CTF_BASE_URL}`,
29
29
  });
30
30
  this.axiosInstance.interceptors.request.use((config) => {
31
31
  // eslint-disable-next-line no-param-reassign
32
32
  config.headers.accept = 'application/json';
33
33
  return config;
34
34
  });
35
- if (CUCUMBER_LOGGING_LEVEL === 'debug') {
35
+ if (CTF_LOGGING_LEVEL === 'debug') {
36
36
  this.axiosInstance.interceptors.request.use(AxiosLogger.requestLogger);
37
37
  this.axiosInstance.interceptors.response.use(AxiosLogger.responseLogger);
38
38
  }
package/lib/api.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,iCAA2F;AAC3F,4CAA2C;AAE3C,+BAA6B;AAG7B,qDAA4C;AAC5C,mCAAkC;AAClC,mCAAqC;AAErC,MAAM,GAAG;IAeP;QACE,MAAM,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,GAAG,OAAO,CAAC,GAAG,CAAA;QAEjE,IAAI,CAAC,cAAc,GAAG;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AA;SAC3C;IACH,CAAC;IAED,kDAAkD;IAC1C,OAAO,CAAC,QAAuB;QACrC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;YAClB,OAAO,IAAI,CAAA;SACZ;QAED,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YACrC,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;gBACnC,OAAO,IAAI,CAAA;aACZ;SACF;aAAM,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE;YACvD,OAAO,IAAI,CAAA;SACZ;QAED,OAAO,KAAK,CAAA;IACd,CAAC;CACF;AAED,kBAAe,GAAG,CAAA","sourcesContent":["import Axios, { AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig } from 'axios'\nimport * as AxiosLogger from 'axios-logger'\nimport * as FormData from 'form-data'\nimport { expect } from 'chai'\n\nimport { ApiPayload, ApiResponse, ApiArrayResponse, Error, V2Response, V3Response } from './api.interfaces'\nimport ResourceStore from './resource_store'\nimport * as Logger from './logger'\nimport { nullOrValue } from './utils'\n\nclass Api {\n public axiosInstance: AxiosInstance\n\n private axiosResponseObject: AxiosResponse | AxiosError\n\n private requestPath: string\n\n private requestMethod: string\n\n // We can't add this to the interceptors because we sometimes\n // need to overwrite this\n private readonly defaultHeaders: object\n\n private headers: object\n\n public constructor() {\n const { CUCUMBER_BASE_URL, CUCUMBER_LOGGING_LEVEL } = process.env\n\n this.defaultHeaders = {\n 'content-type': 'application/json',\n }\n this.headers = this.defaultHeaders\n\n this.axiosInstance = Axios.create({\n baseURL: CUCUMBER_BASE_URL,\n timeout: 4000,\n timeoutErrorMessage: `[Fail] Api request timed out. Is the service running on ${CUCUMBER_BASE_URL}`,\n })\n\n this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => {\n // eslint-disable-next-line no-param-reassign\n config.headers.accept = 'application/json'\n return config\n })\n\n if (CUCUMBER_LOGGING_LEVEL === 'debug') {\n this.axiosInstance.interceptors.request.use(AxiosLogger.requestLogger)\n this.axiosInstance.interceptors.response.use(AxiosLogger.responseLogger)\n }\n }\n\n private isError(response): response is AxiosError {\n return (<AxiosError>this.axiosResponseObject).isAxiosError !== undefined\n }\n\n public isV3Response(data: V2Response | V3Response): data is V3Response {\n if (this.isError(this.axiosResponseObject)) {\n return false\n }\n\n return <V3Response>data.attributes !== undefined\n }\n\n public IsV2Response(data: V2Response | V3Response): data is V2Response {\n return !this.isV3Response(data)\n }\n\n public get errors(): Array<Error> | Array<any> {\n // Response data is never an array if there's errors\n if (Array.isArray(this.response.data)) {\n Logger.error('Attempted to access errors on an array response')\n return null\n }\n\n return this.response.data.errors\n }\n\n public get response(): ApiResponse | ApiArrayResponse {\n if (this.isError(this.axiosResponseObject)) {\n let status\n\n // Status exists in a different place depending on an API request failure or\n // a general AxiosError\n if (Object.prototype.hasOwnProperty.call(this.axiosResponseObject, 'code')) {\n status = this.axiosResponseObject.code\n } else {\n status = this.axiosResponseObject.response.status\n }\n\n return {\n status: parseInt(status, 10),\n data: this.axiosResponseObject?.response?.data,\n } as ApiResponse\n }\n\n return {\n status: this.axiosResponseObject.status,\n // add everything else from the response\n ...this.axiosResponseObject.data,\n }\n }\n\n public async get(endpoint: string, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'GET'\n\n this.axiosResponseObject = await this.axiosInstance\n .get(endpoint, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject) && !this.isEmpty(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async post(endpoint: string, payload: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'POST'\n\n this.axiosResponseObject = await this.axiosInstance\n .post(endpoint, payload, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async form(endpoint: string, form: FormData, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'POST'\n const headers = { ...this.headers, ...form.getHeaders() }\n\n this.axiosResponseObject = await this.axiosInstance\n .post(endpoint, form, { headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async put(endpoint: string, payload: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'PUT'\n\n this.axiosResponseObject = await this.axiosInstance\n .put(endpoint, payload, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n }\n\n public async delete(endpoint: string, payload?: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'DELETE'\n // For some reason, axios delete requests specify the payload and headers in the same object\n const config = {\n ...payload,\n ...{ headers: this.headers },\n }\n\n this.axiosResponseObject = await this.axiosInstance\n .delete(endpoint, config)\n .catch((err) => this.fail(err, abortOnError))\n }\n\n // setHeaders will merge the new headers with the current ones, overwriting if\n // the same key exists. The value will use nullOrValue to properly map strings to literals ('null' => null)\n public setHeaders(newHeaders: object, reset: boolean = false, preserveValueType: boolean = false): void {\n if (reset) {\n this.headers = {}\n }\n\n Object.entries(newHeaders).forEach((h) => {\n let value = h[1]\n if (!preserveValueType) {\n value = nullOrValue(value)\n }\n return Object.assign(this.headers, {\n [h[0]]: value,\n })\n })\n }\n\n public deleteHeader(key: string) {\n delete this.headers[key]\n }\n\n private fail(error: AxiosError, abortOnError: boolean): AxiosError {\n if (abortOnError) {\n this.beautifyError(error)\n expect.fail(error.message)\n }\n\n return error\n }\n\n private beautifyError(error: any): void {\n if (error.response) {\n Logger.error(\n `Failed requesting ${this.requestMethod.toUpperCase()} ${process.env.CTF_BASE_URL}${this.requestPath}`,\n )\n\n if (error.response?.data?.errors !== undefined) {\n Logger.error(error.response?.data?.errors)\n }\n } else {\n Logger.error('[FATAL ERROR]', error, '\\n')\n }\n }\n\n // eslint-disable-next-line class-methods-use-this\n private isEmpty(response: AxiosResponse): boolean {\n if (!response.data) {\n return true\n }\n\n if (Array.isArray(response.data.data)) {\n if (response.data.data.length === 0) {\n return true\n }\n } else if (Object.keys(response.data.data).length === 0) {\n return true\n }\n\n return false\n }\n}\n\nexport default Api\n"]}
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,iCAA2F;AAC3F,4CAA2C;AAE3C,+BAA6B;AAG7B,qDAA4C;AAC5C,mCAAkC;AAClC,mCAAqC;AAErC,MAAM,GAAG;IAeP;QACE,MAAM,EAAE,YAAY,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,GAAG,CAAA;QAEvD,IAAI,CAAC,cAAc,GAAG;YACpB,cAAc,EAAE,kBAAkB;SACnC,CAAA;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,cAAc,CAAA;QAElC,IAAI,CAAC,aAAa,GAAG,eAAK,CAAC,MAAM,CAAC;YAChC,OAAO,EAAE,YAAY;YACrB,OAAO,EAAE,IAAI;YACb,mBAAmB,EAAE,2DAA2D,YAAY,EAAE;SAC/F,CAAC,CAAA;QAEF,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAA0B,EAAE,EAAE;YACzE,6CAA6C;YAC7C,MAAM,CAAC,OAAO,CAAC,MAAM,GAAG,kBAAkB,CAAA;YAC1C,OAAO,MAAM,CAAA;QACf,CAAC,CAAC,CAAA;QAEF,IAAI,iBAAiB,KAAK,OAAO,EAAE;YACjC,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,aAAa,CAAC,CAAA;YACtE,IAAI,CAAC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K,CAAC,CAAA;YACzB,aAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;SAC3B;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAEO,aAAa,CAAC,KAAU;;QAC9B,IAAI,KAAK,CAAC,QAAQ,EAAE;YAClB,MAAM,CAAC,KAAK,CACV,qBAAqB,IAAI,CAAC,aAAa,CAAC,WAAW,EAAE,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY,GAAG,IAAI,CAAC,WAAW,EAAE,CACvG,CAAA;YAED,IAAI,CAAA,MAAA,MAAA,KAAK,CAAC,QAAQ,0CAAE,IAAI,0CAAE,MAAM,MAAK,SAAS,EAAE;gBAC9C,MAAM,CAAC,KAAK,CAAC,MAAA,MAAA,KAAK,CAAC,QAAQ,0CAAE,IAAI,0CAAE,MAAM,CAAC,CAAA;aAC3C;SACF;aAAM;YACL,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;SAC3C;IACH,CAAC;IAED,kDAAkD;IAC1C,OAAO,CAAC,QAAuB;QACrC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;YAClB,OAAO,IAAI,CAAA;SACZ;QAED,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YACrC,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;gBACnC,OAAO,IAAI,CAAA;aACZ;SACF;aAAM,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE;YACvD,OAAO,IAAI,CAAA;SACZ;QAED,OAAO,KAAK,CAAA;IACd,CAAC;CACF;AAED,kBAAe,GAAG,CAAA","sourcesContent":["import Axios, { AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig } from 'axios'\nimport * as AxiosLogger from 'axios-logger'\nimport * as FormData from 'form-data'\nimport { expect } from 'chai'\n\nimport { ApiPayload, ApiResponse, ApiArrayResponse, Error, V2Response, V3Response } from './api.interfaces'\nimport ResourceStore from './resource_store'\nimport * as Logger from './logger'\nimport { nullOrValue } from './utils'\n\nclass Api {\n public axiosInstance: AxiosInstance\n\n private axiosResponseObject: AxiosResponse | AxiosError\n\n private requestPath: string\n\n private requestMethod: string\n\n // We can't add this to the interceptors because we sometimes\n // need to overwrite this\n private readonly defaultHeaders: object\n\n private headers: object\n\n public constructor() {\n const { CTF_BASE_URL, CTF_LOGGING_LEVEL } = process.env\n\n this.defaultHeaders = {\n 'content-type': 'application/json',\n }\n this.headers = this.defaultHeaders\n\n this.axiosInstance = Axios.create({\n baseURL: CTF_BASE_URL,\n timeout: 4000,\n timeoutErrorMessage: `[Fail] Api request timed out. Is the service running on ${CTF_BASE_URL}`,\n })\n\n this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => {\n // eslint-disable-next-line no-param-reassign\n config.headers.accept = 'application/json'\n return config\n })\n\n if (CTF_LOGGING_LEVEL === 'debug') {\n this.axiosInstance.interceptors.request.use(AxiosLogger.requestLogger)\n this.axiosInstance.interceptors.response.use(AxiosLogger.responseLogger)\n }\n }\n\n private isError(response): response is AxiosError {\n return (<AxiosError>this.axiosResponseObject).isAxiosError !== undefined\n }\n\n public isV3Response(data: V2Response | V3Response): data is V3Response {\n if (this.isError(this.axiosResponseObject)) {\n return false\n }\n\n return <V3Response>data.attributes !== undefined\n }\n\n public IsV2Response(data: V2Response | V3Response): data is V2Response {\n return !this.isV3Response(data)\n }\n\n public get errors(): Array<Error> | Array<any> {\n // Response data is never an array if there's errors\n if (Array.isArray(this.response.data)) {\n Logger.error('Attempted to access errors on an array response')\n return null\n }\n\n return this.response.data.errors\n }\n\n public get response(): ApiResponse | ApiArrayResponse {\n if (this.isError(this.axiosResponseObject)) {\n let status\n\n // Status exists in a different place depending on an API request failure or\n // a general AxiosError\n if (Object.prototype.hasOwnProperty.call(this.axiosResponseObject, 'code')) {\n status = this.axiosResponseObject.code\n } else {\n status = this.axiosResponseObject.response.status\n }\n\n return {\n status: parseInt(status, 10),\n data: this.axiosResponseObject?.response?.data,\n } as ApiResponse\n }\n\n return {\n status: this.axiosResponseObject.status,\n // add everything else from the response\n ...this.axiosResponseObject.data,\n }\n }\n\n public async get(endpoint: string, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'GET'\n\n this.axiosResponseObject = await this.axiosInstance\n .get(endpoint, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject) && !this.isEmpty(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async post(endpoint: string, payload: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'POST'\n\n this.axiosResponseObject = await this.axiosInstance\n .post(endpoint, payload, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async form(endpoint: string, form: FormData, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'POST'\n const headers = { ...this.headers, ...form.getHeaders() }\n\n this.axiosResponseObject = await this.axiosInstance\n .post(endpoint, form, { headers })\n .catch((err) => this.fail(err, abortOnError))\n\n if (!this.isError(this.axiosResponseObject)) {\n ResourceStore.Store(this.axiosResponseObject.data)\n }\n }\n\n public async put(endpoint: string, payload: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'PUT'\n\n this.axiosResponseObject = await this.axiosInstance\n .put(endpoint, payload, { headers: this.headers })\n .catch((err) => this.fail(err, abortOnError))\n }\n\n public async delete(endpoint: string, payload?: ApiPayload, abortOnError: boolean = true): Promise<void> {\n this.requestPath = endpoint\n this.requestMethod = 'DELETE'\n // For some reason, axios delete requests specify the payload and headers in the same object\n const config = {\n ...payload,\n ...{ headers: this.headers },\n }\n\n this.axiosResponseObject = await this.axiosInstance\n .delete(endpoint, config)\n .catch((err) => this.fail(err, abortOnError))\n }\n\n // setHeaders will merge the new headers with the current ones, overwriting if\n // the same key exists. The value will use nullOrValue to properly map strings to literals ('null' => null)\n public setHeaders(newHeaders: object, reset: boolean = false, preserveValueType: boolean = false): void {\n if (reset) {\n this.headers = {}\n }\n\n Object.entries(newHeaders).forEach((h) => {\n let value = h[1]\n if (!preserveValueType) {\n value = nullOrValue(value)\n }\n return Object.assign(this.headers, {\n [h[0]]: value,\n })\n })\n }\n\n public deleteHeader(key: string) {\n delete this.headers[key]\n }\n\n private fail(error: AxiosError, abortOnError: boolean): AxiosError {\n if (abortOnError) {\n this.beautifyError(error)\n expect.fail(error.message)\n }\n\n return error\n }\n\n private beautifyError(error: any): void {\n if (error.response) {\n Logger.error(\n `Failed requesting ${this.requestMethod.toUpperCase()} ${process.env.CTF_BASE_URL}${this.requestPath}`,\n )\n\n if (error.response?.data?.errors !== undefined) {\n Logger.error(error.response?.data?.errors)\n }\n } else {\n Logger.error('[FATAL ERROR]', error, '\\n')\n }\n }\n\n // eslint-disable-next-line class-methods-use-this\n private isEmpty(response: AxiosResponse): boolean {\n if (!response.data) {\n return true\n }\n\n if (Array.isArray(response.data.data)) {\n if (response.data.data.length === 0) {\n return true\n }\n } else if (Object.keys(response.data.data).length === 0) {\n return true\n }\n\n return false\n }\n}\n\nexport default Api\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"resource.js","sourceRoot":"","sources":["../src/resource.ts"],"names":[],"mappings":";;;AAAA,mCAAqC;AAcrC,MAAa,QAAQ;IAGnB,YAAY,IAAY,EAAE,UAAe;QACvC,IAAI,CAAC,IAAI,mBACP,IAAI,IACD,UAAU,CACd,CAAA;QACD,iDAAiD;QACjD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,gFAAgF;IAChF,gFAAgF;IACzE,SAAS,CAAC,gBAA6B;;QACrC,MAAA,IAA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CAAC,CAAA;SAC/C;QAED,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC;CACF;AApDD,4BAoDC","sourcesContent":["import { nullOrValue } from './utils'\n\ninterface Attributes {\n [key: string]: any\n}\n\nexport interface ResourceData {\n id: string\n type: string\n attributes?: Attributes\n // Allow for V2 resources\n [key: string]: any\n}\n\nexport class Resource {\n public data: ResourceData\n\n constructor(type: string, attributes: any) {\n this.data = {\n type,\n ...attributes,\n }\n // eslint-disable-next-line no-constructor-return\n return this\n }\n\n // Returns the resource as a payload, removing all attributes that cannot appear\n // in a payload, as well as converting values from string \"null\" to literal null\n public asPayload(updateAttributes?: Attributes) {\n delete this.data?.meta\n delete this.data?.relationships\n\n if (!updateAttributes) {\n return this.data\n }\n\n const clonedUpdate = updateAttributes\n\n // Loop through all update attributes and nullify the key\n // if its value is string \"null\"\n Object.keys(updateAttributes).forEach((key) => {\n clonedUpdate[key] = nullOrValue(updateAttributes[key])\n })\n\n const clone = this.data\n\n // For those test cases where you want to remove or change these\n // attributes to assert an error\n if (Object.prototype.hasOwnProperty.call(clonedUpdate, 'id')) {\n this.data.id = clonedUpdate.id\n delete clonedUpdate.id // so we don't put it in the attributes\n }\n\n if (Object.prototype.hasOwnProperty.call(clonedUpdate, 'type')) {\n this.data.type = updateAttributes.type\n delete clonedUpdate.type // so we don't put it in the attributes\n }\n\n if (Object.prototype.hasOwnProperty.call(this.data, 'attributes')) {\n this.data.attributes = Object.assign(clone.attributes, clonedUpdate)\n } else {\n this.data = Object.assign(clone, clonedUpdate)\n }\n\n return this.data\n }\n}\n"]}
1
+ {"version":3,"file":"resource.js","sourceRoot":"","sources":["../src/resource.ts"],"names":[],"mappings":";;;AAAA,mCAAqC;AAcrC,MAAa,QAAQ;IAGnB,YAAY,IAAY,EAAE,UAAe;QACvC,IAAI,CAAC,IAAI,mBACP,IAAI,IACD,UAAU,CACd,CAAA;QACD,iDAAiD;QACjD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,gFAAgF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AAC,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,YAAY,CAAC,CAAA;SAC/C;QAED,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC;CACF;AApDD,4BAoDC","sourcesContent":["import { nullOrValue } from './utils'\n\ninterface Attributes {\n [key: string]: any\n}\n\nexport interface ResourceData {\n id: string\n type: string\n attributes?: Attributes\n // Allow for resources that don't conform to JSONAPI\n [key: string]: any\n}\n\nexport class Resource {\n public data: ResourceData\n\n constructor(type: string, attributes: any) {\n this.data = {\n type,\n ...attributes,\n }\n // eslint-disable-next-line no-constructor-return\n return this\n }\n\n // Returns the resource as a payload, removing all attributes that cannot appear\n // in a payload, as well as converting values from string \"null\" to literal null\n public asPayload(updateAttributes?: Attributes) {\n delete this.data?.meta\n delete this.data?.relationships\n\n if (!updateAttributes) {\n return this.data\n }\n\n const clonedUpdate = updateAttributes\n\n // Loop through all update attributes and nullify the key\n // if its value is string \"null\"\n Object.keys(updateAttributes).forEach((key) => {\n clonedUpdate[key] = nullOrValue(updateAttributes[key])\n })\n\n const clone = this.data\n\n // For those test cases where you want to remove or change these\n // attributes to assert an error\n if (Object.prototype.hasOwnProperty.call(clonedUpdate, 'id')) {\n this.data.id = clonedUpdate.id\n delete clonedUpdate.id // so we don't put it in the attributes\n }\n\n if (Object.prototype.hasOwnProperty.call(clonedUpdate, 'type')) {\n this.data.type = updateAttributes.type\n delete clonedUpdate.type // so we don't put it in the attributes\n }\n\n if (Object.prototype.hasOwnProperty.call(this.data, 'attributes')) {\n this.data.attributes = Object.assign(clone.attributes, clonedUpdate)\n } else {\n this.data = Object.assign(clone, clonedUpdate)\n }\n\n return this.data\n }\n}\n"]}
@@ -1,8 +1,8 @@
1
1
  import { ApiResponse, ApiArrayResponse } from './api.interfaces';
2
- import { ResourceData, Resource } from './resource';
2
+ import { Resource } from './resource';
3
3
  export default class ResourceStore {
4
4
  private static resources;
5
- static FetchByType(type: string): ResourceData;
5
+ static FetchByType(type: string): Resource;
6
6
  /**
7
7
  * Finds the resource by looking for the key and value
8
8
  * @param type {string} the type of resource
@@ -11,7 +11,7 @@ export default class ResourceStore {
11
11
  * @return {Resource | undefined}
12
12
  */
13
13
  static Find(type: string, key: string, value: any): Resource;
14
- static LastCreated(type: string): ResourceData;
14
+ static LastCreated(type: string): Resource;
15
15
  private static isArrayResponse;
16
16
  static Store(responses: ApiResponse | ApiArrayResponse): void;
17
17
  static Clear(): void;
@@ -16,12 +16,12 @@ class ResourceStore {
16
16
  static Find(type, key, value) {
17
17
  const resources = this.resources[type] || [];
18
18
  return resources.find((resource) => {
19
- let target = resource;
19
+ let target = resource.data;
20
20
  if (key.indexOf('.') > -1) {
21
21
  target = key.split('.').reduce((original, i) => original === null || original === void 0 ? void 0 : original[i], target);
22
22
  }
23
23
  else {
24
- target = resource[key];
24
+ target = resource.data[key];
25
25
  }
26
26
  return target === value;
27
27
  });
@@ -48,12 +48,12 @@ class ResourceStore {
48
48
  ;
49
49
  responses.data.forEach((data) => {
50
50
  const resource = new resource_1.Resource(type, data);
51
- this.resources[type].push(resource.data);
51
+ this.resources[type].push(resource);
52
52
  });
53
53
  }
54
54
  else {
55
55
  const resource = new resource_1.Resource(type, responses.data);
56
- this.resources[type].push(resource.data);
56
+ this.resources[type].push(resource);
57
57
  }
58
58
  }
59
59
  static Clear() {
@@ -1 +1 @@
1
- {"version":3,"file":"resource_store.js","sourceRoot":"","sources":["../src/resource_store.ts"],"names":[],"mappings":";;AAAA,+BAA6B;AAG7B,yCAAmD;AAEnD,MAAqB,aAAa;IAIzB,MAAM,CAAC,WAAW,CAAC,IAAY;QACpC,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;IAC7B,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,IAAI,CAAC,IAAY,EAAE,GAAW,EAAE,KAAU;QACtD,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,CAAA;QAE5C,OAA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,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;gBACnE,MAAM,QAAQ,GAAG,IAAI,mBAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;gBACzC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;YAC1C,CAAC,CAAC,CAAA;SACH;aAAM;YACL,MAAM,QAAQ,GAAG,IAAI,mBAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,CAAA;YACnD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;SACzC;IACH,CAAC;IAEM,MAAM,CAAC,KAAK;QACjB,IAAI,CAAC,SAAS,GAAG,EAAE,CAAA;IACrB,CAAC;IAED,2FAA2F;IAC3F,uCAAuC;IAC/B,MAAM,CAAC,eAAe,CAAC,SAAyC;;QACtE,IAAI,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,EAAE;YACnC,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE;gBACnC,aAAM,CAAC,IAAI,CAAC,8CAA8C,CAAC,CAAA;aAC5D;YACD,OAAO,MAAA,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,0CAAE,IAAI,CAAA;SAC/B;QAED,IAAI,SAAS,CAAC,IAAI,KAAK,SAAS,EAAE;YAChC,aAAM,CAAC,IAAI,CAAC,sCAAsC,CAAC,CAAA;SACpD;QACD,OAAQ,SAAyB,CAAC,IAAI,CAAC,IAAI,CAAA;IAC7C,CAAC;;AA/ED,sFAAsF;AACvE,uBAAS,GAAwB,EAAE,CAAA;kBAF/B,aAAa","sourcesContent":["import { expect } from 'chai'\n\nimport { ApiResponse, ApiArrayResponse, V3Response, V2Response } from './api.interfaces'\nimport { ResourceData, Resource } from './resource'\n\nexport default class ResourceStore {\n // Contains a list of resources (singular keys for the resource type eg: flow/product)\n private static resources: Array<ResourceData> = []\n\n public static FetchByType(type: string): ResourceData {\n return this.resources[type]\n }\n\n /**\n * Finds the resource by looking for the key and value\n * @param type {string} the type of resource\n * @param key {string} a JSON path string. Eg: attributes.name\n * @param value {any} the value to look for\n * @return {Resource | undefined}\n */\n public static Find(type: string, key: string, value: any): Resource {\n const resources = this.resources[type] || []\n\n return resources.find((resource) => {\n let target = resource\n if (key.indexOf('.') > -1) {\n target = key.split('.').reduce((original, i) => original?.[i], target)\n } else {\n target = resource[key]\n }\n\n return target === value\n })\n }\n\n /*\n Returns the last created resource\n @param type string The type of the resource, eg: product\n @return The Resource\n */\n public static LastCreated(type: string): ResourceData {\n const resources = this.resources[type] || []\n return resources[resources.length - 1]\n }\n\n private static isArrayResponse(responses: ApiResponse | ApiArrayResponse): boolean {\n return (responses as ApiArrayResponse)?.data?.length !== undefined\n }\n\n public static Store(responses: ApiResponse | ApiArrayResponse): void {\n const type = this.getResourceType(responses)\n\n if (!Object.prototype.hasOwnProperty.call(this.resources, type)) {\n this.resources[type] = []\n }\n\n if (this.isArrayResponse(responses)) {\n ;(responses.data as Array<V3Response | V2Response>).forEach((data) => {\n const resource = new Resource(type, data)\n this.resources[type].push(resource.data)\n })\n } else {\n const resource = new Resource(type, responses.data)\n this.resources[type].push(resource.data)\n }\n }\n\n public static Clear(): void {\n this.resources = []\n }\n\n // Gets the type of the resource, taking in to account an array API response, where it will\n // infer the type from the first object\n private static getResourceType(responses: ApiResponse | ApiArrayResponse): string {\n if (this.isArrayResponse(responses)) {\n if (responses.data[0] === undefined) {\n expect.fail('Could not find any resources in the response')\n }\n return responses.data[0]?.type\n }\n\n if (responses.data === undefined) {\n expect.fail('Could not a resource in the response')\n }\n return (responses as ApiResponse).data.type\n }\n}\n"]}
1
+ {"version":3,"file":"resource_store.js","sourceRoot":"","sources":["../src/resource_store.ts"],"names":[],"mappings":";;AAAA,+BAA6B;AAG7B,yCAAqC;AAErC,MAAqB,aAAa;IAIzB,MAAM,CAAC,WAAW,C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I,CAAA;IAC7C,CAAC;;AA/ED,sFAAsF;AACvE,uBAAS,GAAoB,EAAE,CAAA;kBAF3B,aAAa","sourcesContent":["import { expect } from 'chai'\n\nimport { ApiResponse, ApiArrayResponse, V3Response, V2Response } from './api.interfaces'\nimport { Resource } from './resource'\n\nexport default class ResourceStore {\n // Contains a list of resources (singular keys for the resource type eg: flow/product)\n private static resources: Array<Resource> = []\n\n public static FetchByType(type: string): Resource {\n return this.resources[type]\n }\n\n /**\n * Finds the resource by looking for the key and value\n * @param type {string} the type of resource\n * @param key {string} a JSON path string. Eg: attributes.name\n * @param value {any} the value to look for\n * @return {Resource | undefined}\n */\n public static Find(type: string, key: string, value: any): Resource {\n const resources = this.resources[type] || []\n\n return resources.find((resource) => {\n let target = resource.data\n if (key.indexOf('.') > -1) {\n target = key.split('.').reduce((original, i) => original?.[i], target)\n } else {\n target = resource.data[key]\n }\n\n return target === value\n })\n }\n\n /*\n Returns the last created resource\n @param type string The type of the resource, eg: product\n @return The Resource\n */\n public static LastCreated(type: string): Resource {\n const resources = this.resources[type] || []\n return resources[resources.length - 1]\n }\n\n private static isArrayResponse(responses: ApiResponse | ApiArrayResponse): boolean {\n return (responses as ApiArrayResponse)?.data?.length !== undefined\n }\n\n public static Store(responses: ApiResponse | ApiArrayResponse): void {\n const type = this.getResourceType(responses)\n\n if (!Object.prototype.hasOwnProperty.call(this.resources, type)) {\n this.resources[type] = []\n }\n\n if (this.isArrayResponse(responses)) {\n ;(responses.data as Array<V3Response | V2Response>).forEach((data) => {\n const resource = new Resource(type, data)\n this.resources[type].push(resource)\n })\n } else {\n const resource = new Resource(type, responses.data)\n this.resources[type].push(resource)\n }\n }\n\n public static Clear(): void {\n this.resources = []\n }\n\n // Gets the type of the resource, taking in to account an array API response, where it will\n // infer the type from the first object\n private static getResourceType(responses: ApiResponse | ApiArrayResponse): string {\n if (this.isArrayResponse(responses)) {\n if (responses.data[0] === undefined) {\n expect.fail('Could not find any resources in the response')\n }\n return responses.data[0]?.type\n }\n\n if (responses.data === undefined) {\n expect.fail('Could not a resource in the response')\n }\n return (responses as ApiResponse).data.type\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elasticpath/component-test-framework",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "A framework for component tests written using ts-flow",
5
5
  "main": "./lib/index.js",
6
6
  "directories": {