@oracle/oraclejet-jest-preset 12.0.0

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/jest-preset.js ADDED
@@ -0,0 +1,3 @@
1
+ // This file must be at the root of the package to be able to be used in jest.
2
+ // See https://jestjs.io/docs/en/configuration#preset-string
3
+ module.exports = require('./src/preset');
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@oracle/oraclejet-jest-preset",
3
+ "version": "12.0.0",
4
+ "description": "JET preset for Jest testing",
5
+ "main": "jest-preset.js",
6
+ "author": "",
7
+ "license": "UPL-1.0",
8
+ "dependencies": {
9
+ "babel-jest": "^27.4.5",
10
+ "babel-plugin-transform-amd-to-commonjs": "^1.4.0",
11
+ "css": "^3.0.0",
12
+ "jest-preset-preact": "^4.0.2",
13
+ "jest-raw-loader": "^1.0.1",
14
+ "preact-render-to-string": "^5.1.16"
15
+ },
16
+ "peerDependencies": {
17
+ "jest": "27.x",
18
+ "@oracle/oraclejet": ">=12.x"
19
+ },
20
+ "files": [
21
+ "*.js",
22
+ "src/**"
23
+ ]
24
+ }
package/src/babel.js ADDED
@@ -0,0 +1,26 @@
1
+ const { default: babelJest } = require('babel-jest');
2
+
3
+ module.exports = babelJest.createTransformer({
4
+ presets: [
5
+ [
6
+ '@babel/preset-typescript',
7
+ {
8
+ jsxPragma: 'h',
9
+ },
10
+ ],
11
+ '@babel/preset-env',
12
+ ],
13
+ plugins: [
14
+ 'babel-plugin-transform-amd-to-commonjs',
15
+ [
16
+ '@babel/plugin-transform-react-jsx',
17
+ {
18
+ pragma: 'h',
19
+ pragmaFrag: 'Fragment',
20
+ },
21
+ ],
22
+ '@babel/plugin-proposal-class-properties',
23
+ ],
24
+ babelrc: false,
25
+ configFile: false,
26
+ });
@@ -0,0 +1,9 @@
1
+ // This loader is to be used when importing a JSON file (as done by CCA's loader.ts)
2
+ // expects that the content will come as text. NodeJS's behavior is to parse JSON
3
+ // files and return the object from the module, causing double-parsing when passed
4
+ // to JSON.parse().
5
+ // This loader escapes the double-quotes so that Node's JSON.parse() treats it
6
+ // as a single string key.
7
+ module.exports = {
8
+ process: content => `"` + content.replace(/"/g, `\\"`).replace(/\n/g, '') + `"`
9
+ }
@@ -0,0 +1,45 @@
1
+ // This loader mimics ojL10n's behavior of returning the contents of the 'root'
2
+ // property from JET bundles.
3
+
4
+ const vm = require('vm');
5
+
6
+ /*
7
+ * Create a context in which the resource bundle's script will run.
8
+ * AMD resource bundles are defined in one of two structures:
9
+ * - define({ resource_keys... })
10
+ * - define(['require', 'exports'], function(require, exports) { })
11
+ *
12
+ * For the 2nd structure (with callback), the resource bundle can be returned
13
+ * from the callback or assigned to the "exports" parameter.
14
+ *
15
+ * Return the bundle from our define() so that it can be uniformly extracted
16
+ */
17
+ const context = vm.createContext({
18
+ define: function(rb) {
19
+ // define({ ... })
20
+ if (arguments.length === 1) {
21
+ return rb;
22
+ }
23
+ // define(['require', 'exports'], function(require, exports))
24
+ const exp = {};
25
+ // pass exports for every argument
26
+ const args = new Array(arguments[0].length);
27
+ const ret = arguments[1](...args.fill(exp));
28
+ // AMD may return the value or assign to 'exports'
29
+ const resource = ret || exp;
30
+ return resource.default || resource;
31
+ }
32
+ });
33
+
34
+ module.exports = {
35
+ process(content, filename) {
36
+ // "content" is the resource bundle's script source
37
+ // Since the script runs in our context, assign it to "this.rb" so that we
38
+ // can retrieve it
39
+ const script = new vm.Script(`this.rb = ${content}`);
40
+ script.runInContext(context);
41
+ // app resource bundles are underneath 'root'
42
+ // otherwise return entire bundle
43
+ return `module.exports = ${JSON.stringify(context.rb.root || context.rb)}`;
44
+ }
45
+ }
@@ -0,0 +1 @@
1
+ module.exports = {};
@@ -0,0 +1,9 @@
1
+ const LocaleData = require("../../../oraclejet/dist/js/libs/oj/debug/ojlocaledata");
2
+ const rootBundle = require("ojL10n!ojtranslations/nls/localeElements");
3
+ const enBundle = require("ojL10n!ojtranslations/nls/en/localeElements");
4
+
5
+ Object.assign(enBundle.main.en, rootBundle, {
6
+ _ojLocale_: "en",
7
+ });
8
+ LocaleData.setBundle(enBundle.main.en);
9
+ module.exports = LocaleData;
@@ -0,0 +1,57 @@
1
+ const css = require('css');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+
5
+ let rules;
6
+ let cssVars = [];
7
+ const fontFamilyJsons = {};
8
+ function parseRules() {
9
+ if (!rules) {
10
+ const cssPath = path.resolve(__dirname, '../../../oraclejet/dist/css/stable/oj-stable.css');
11
+ const contents = fs.readFileSync(cssPath).toString();
12
+ rules = css.parse(contents).stylesheet.rules.filter((rule) => rule.selectors);
13
+ }
14
+ }
15
+
16
+ function parseJSONFromFontFamily(className) {
17
+ parseRules();
18
+ let config = fontFamilyJsons[className];
19
+ if (!config) {
20
+ const selector = `.${className}`;
21
+ // Find the rule matching selector and declaration with 'font-family'
22
+ const rule = rules.find(
23
+ (rule) =>
24
+ rule.selectors.includes(selector) &&
25
+ rule.declarations.find((decl) => decl.property === 'font-family')
26
+ );
27
+ const ff = rule.declarations.find((decl) => decl.property === 'font-family');
28
+ // Remove !important and start/ending quotes
29
+ const ffValue = ff.value.replace(/\s*!important;?/, '').replace(/^'/, '').replace(/'$/, '')
30
+ config = fontFamilyJsons[className] = JSON.parse(ffValue);
31
+ }
32
+ return config;
33
+ }
34
+
35
+ function getCachedCSSVarValues(names) {
36
+ parseRules();
37
+ if (!cssVars.length) {
38
+ // Find all :root declarations
39
+ rules.forEach((rule) => {
40
+ if (rule.selectors.includes(':root')) {
41
+ cssVars = cssVars.concat(rule.declarations);
42
+ }
43
+ });
44
+ }
45
+ const matches = cssVars.filter((decl => names.includes(decl.property)));
46
+ // Remove leading/trailing quotes
47
+ return matches.map((match) => match.value.replace(/^'/, '').replace(/'$/, ''));
48
+ }
49
+
50
+ module.exports = {
51
+ clearCache: function () {},
52
+ getThemeName: function () {},
53
+ getThemeTargetPlatform: function () {},
54
+ parseJSONFromFontFamily,
55
+ verifyThemeVersion: function () {},
56
+ getCachedCSSVarValues
57
+ };
@@ -0,0 +1,5 @@
1
+ const Translation = require("../../../oraclejet/dist/js/libs/oj/debug/ojtranslation");
2
+ const rootTranslations = require("ojL10n!ojtranslations/nls/ojtranslations");
3
+
4
+ Translation.setBundle(rootTranslations);
5
+ module.exports = Translation;
package/src/preset.js ADDED
@@ -0,0 +1,45 @@
1
+ const jestPreset = require('jest-preset-preact');
2
+ const path = require('path');
3
+
4
+ const mappedModules = {
5
+ // Noop style files
6
+ '^.+\\.(css|sass|scss|less)$': 'identity-obj-proxy',
7
+ '^css!.*': 'identity-obj-proxy',
8
+ '^ojcss!.*': 'identity-obj-proxy',
9
+ // Remove text! prefix and use loaders to transform content to text for things
10
+ // typically loaded with this plugin (text & html)
11
+ '^text!(.*)': '$1',
12
+ // Mock modules
13
+ touchr: path.resolve(__dirname, 'mocks', 'blank'),
14
+ ojdnd: path.resolve(__dirname, 'mocks', 'blank'),
15
+ 'ojs/(.*)': [
16
+ // Resolve ojs/* to any mock modules first
17
+ path.resolve(__dirname, 'mocks', '$1'),
18
+ '<rootDir>/node_modules/@oracle/oraclejet/dist/js/libs/oj/debug/$1'
19
+ ],
20
+ 'ojL10n!./(.*)': './$1',
21
+ 'ojL10n!ojtranslations/nls/(.*)':
22
+ '<rootDir>/node_modules/@oracle/oraclejet/dist/js/libs/oj/resources/nls/$1',
23
+ 'jqueryui-amd/(.*)':
24
+ '<rootDir>/node_modules/@oracle/oraclejet/dist/js/libs/jquery/jqueryui-amd-1.13.0/$1'
25
+ };
26
+
27
+ Object.assign(jestPreset, {
28
+ modulePaths: ['<rootDir>/node_modules/@oracle/oraclejet/dist/js/libs/oj/'],
29
+ transform: {
30
+ 'resources\\/nls\\/': require.resolve('./loaders/ojL10n-loader'),
31
+ // Use our own Babel configuration
32
+ '^.+\\.(mjs|js|jsx|ts|tsx)$': path.resolve(__dirname, 'babel.js'),
33
+ // Handle loader.ts which loads component.json and expects string instead of
34
+ // JSON object
35
+ 'component\\.json$': require.resolve('./loaders/json-text-loader'),
36
+ '\\.html$': 'jest-raw-loader',
37
+ },
38
+ transformIgnorePatterns: [
39
+ '/node_modules/(?!(@oracle/oraclejet)/)',
40
+ '^.+\\.(css|sass|scss|less)$'
41
+ ]
42
+ });
43
+ Object.assign(jestPreset.moduleNameMapper, mappedModules);
44
+
45
+ module.exports = jestPreset;